I’d like to do something like this:
#define NEED3ARGS(a1,a2,a3) ( "[" #a1 " + " #a2 " + " #a3 "]" )
#define MULTIARG() ARG1, ARG2, ARG3
NEED3ARGS( MULTIARG() )
And I expected it to output something like:
( "[" "ARG1" " + " "ARG2" " + " "ARG3" "]" )
But instead I have:
$ cpp multiarg.c
# 1 "multiarg.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "multiarg.c"
multiarg.c:4:23: error: macro "NEED3ARGS" requires 3 arguments, but only 1 given
NEED3ARGS
Is there a way to do what I want using ANSI-C/GNU GCC and the C preprocessor?
Thanks!
You need some indirection. With C99:
(C99 isn’t strictly required; you can replace the variadic macro with a fixed-arity macro.)
If you need to compile your source using Visual C++, you’ll need even more indirection (because of a compiler bug):
As for why indirection is needed: a macro argument is not evaluated and macro-replaced until it is substituted into the replacement list. So, when you try
NEED3ARGS(MULTIARG()),MULTIARG()will not be evaluated until after macro invocation has begun, so it’s treated as a single argument.The
INVOKE_NEED3ARGSmacro ensures that its arguments are completely evaluated beforeNEED3ARGSis invoked. The__VA_ARGS__is substituted by the macro-replaced arguments toINVOKE_NEED3ARGS, which isARG1, ARG2, ARG3, thenNEED3ARGSis invoked with those arguments.