Consider this code:
#define N_ 0
#define N_X 1
#define M(a) N_
M(arg)X; // #1 -- I'd like this to expand to N_X, and ultimately 1; but it's 0X instead
M(arg); // #2 -- this should and does expand to 0
The problem with #1 is that after expanding M(), the result contains N_, and before concatenating it with X, the preprocessor finds and expands it.
Can I somehow delay this re-scanning of the result for further macros, such that the preprocessor finds N_X instead of N_?
First there is a different between
N_ XandN_X. The first are two tokens. To form one token you have to use the token pasting operator##, but this operator inhibits macro expansion, so this:Causes a compile error, because its trying to paste
M(a)and notN_. You can allow for macros to expand before pasting by using an extra level of macros(this is really commonly used macro):However, in your case, this still won’t work:
That is because, you are using object macros, rather than function macros. If you change it to function macros, it will work how you want:
Function macros are more powerful and you can delay the expansion of them. Here’s how you can delay them for one scan:
Delaying macro expansion like this is one of the ways recursion can be implemented in the preprocessor.