I am using the P99_FOR macro defined in P99 in my C99 code in order to iterate over VA_ARGS. It works perfectly.
P99_FOR(NAME, N, OP, FUNC,...)
Now I want to migrate to C++11 and I was wondering if there is any macro similar to P99_FOR.
Here is my code in C99:
#ifndef __cplusplus
#include "p99/p99.h"
#undef P00_VASSIGN
#define P00_VASSIGN(NAME, X, I) NAME[I] = X
#define FOREACH(x, y, z, u, ...) P99_FOR(x, y, z, u, __VA_ARGS__);
#else
#define FOREACH(x, y, z, u, ...) ??? // C++ equivalent
#endif
#define set_OCTET_STRING(type, numParams, ...) { \
FOREACH(type, numParams, P00_SEP, P00_VASSIGN, __VA_ARGS__); \
}
For example set_OCTET_STRING(myVar->speed, 3, 34, 10, 11)
will expand to:
myVar->speed[0] = 34; myVar->speed[1] = 10; myVar->speed[2] = 11;
You have a couple of ways to go. If you can get iterators to your data, you can use
std::accumulate
.The example is taken from the docs:
If your arguments are not iterateable, e.g. single variables on the stack, you have to built it on your own, using variardic templates: