Reputation: 843
I recently came across this macro:
#define EAT(...)
#define STRIP(x) EAT x
STRIP( (1) 2 ) \\ expands to 2
Now can someone please explain what is going on?
How this EAT x expands?
What that parenthesis'(1)' do?
Why I can´t do the reverse like STRIP( 1 (2) ) ?
My initial intentions was spliting one argument in a macro like SPLIT(1 2) to expands to 1,2 there is a way?
Upvotes: 2
Views: 81
Reputation: 9648
Lets step through the substitutions:
STRIP( (1) 2 )
EAT (1) 2
2
For the second example:
STRIP( 1 (2) )
EAT 1 (2)
//error, EAT is a macro so it needs ()
Upvotes: 3
Reputation: 171383
EAT
is a function-like macro, which means it must be used like EAT(something)
, and it expands to nothing.
So STRIP((1) 2)
expands to EAT (1) 2
which expands to 2
What that parenthesis'(1)' do?
it forms EAT(1)
which gets expanded
Why I can´t do the reverse like STRIP( 1 (2) ) ?
because that forms EAT 1 (2)
and you can't use EAT
like that.
Upvotes: 4