I am using nested macro under gcc-9.3.1
.
Here is the code.
#define A(_name) _name, _name, _name
#define P(_1, _2, _name, ...) _name
#define B(...) P(__VA_ARGS__, A(something))(__VA_ARGS__)
B(k)
I expected the B(k)
converted to P(k, something, something, something)(k)
at first and then converted into something(k)
.
However, compilier told me that two arguments were too few for P
, which meant that A(something)
was not unfolded.
Why is that? And how can I make it unfolded?
CodePudding user response:
B(k)
expands to P(k, A(something))(k)
which then undergoes recursive expansion. The first thing it finds there is P
, which doesn't have enough arguments.
If you want this to work the way I think you do, you need to arrange for A
to expand before P
. You can do that by adding explicit indirect EXPAND
macros:
#define EXPAND(...) __VA_ARGS__
#define B(...) EXPAND(P EXPAND((__VA_ARGS__, A(something))))(__VA_ARGS__)
This way, P
won't be recognized as a macro (no following (
) until after the inner EXPAND
is expanded (which will also expand A). You need the outer EXPAND
to explicitly expand P
afterwards.