简体   繁体   中英

Strange Macro Expansion

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?

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.

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 ()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM