#define PP_ARG0_(arg0, ...) arg0
#define PP_REST_(arg0, ...) __VA_ARGS__
#define PP_ARG0(args) PP_ARG0_ args
#define PP_REST(args) PP_REST_ args
#define FUNCTION(name) void name();
#define FUNCTION_TABLE(...) \
FUNCTION(PP_ARG0((__VA_ARGS__))) \
FUNCTION_TABLE(PP_REST((__VA_ARGS__))) \
test code:
FUNCTION_TABLE(f1, f2,f3,testA,testB,testC);
Obviously, because of recursive expansion it will only declare void f1(); and the rest won’t be expanded:
void f1(); FUNCTION_TABLE(f2,f3,testA,testB,testC);
What kind of trick can I use to achieve recursive expansion in this case? The problem is that I need to support MANY arguments (up 100) and I absolutely cannot use boost.
Here’s the answer in case somebody wants to do the same.
where
PP_NARGis the macro that counts number of arguments andPP_JOINis the macro that joins tokens (that isPP_JOIN(a,b) => ab). You’ll also need to patch thatPP_NARGif you want to be able to process more than 64 arguments.Now, back to the original question. Solution using the
PP_TRANSFORMis:if you want to generate c++ implementation functions then you can use that opaque x parameter of
PP_TRANSFORM:All this works equally well with GCC and MSVC preprocessors; PP_TRANSFORM_NN doesn’t use
__VA_ARGS__to avoid separate implementations of 100 defines for GCC and MSVC