Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro Meta Programming

I know this is probably either bad or impossible, but since this isn't a recursive macro I think it should be possible.

#define FOO 15
#define MAKE_BAR(x) BAR_##x
#define MY_FOO_BAR MAKE_BAR(FOO)

I'd like MY_FOO_BAR to evaluate to BAR_15. Is there a way to tell the preprocessor to evaluate FOO before passing it into MAKE_BAR?

like image 860
IdeaHat Avatar asked May 21 '26 21:05

IdeaHat


1 Answers

You need another level of macro calls:

#define FOO 15
#define MAKE_BAR_INNER(x) BAR_##x
#define MAKE_BAR(x) MAKE_BAR_INNER(x)
#define MY_FOO_BAR MAKE_BAR(FOO)

This is because of how parameters are handled during functional macro expansion. The ## concatenation operator prevents parameter expansion, so you must "force" expansion by adding another "layer".

like image 175
Drew McGowen Avatar answered May 24 '26 13:05

Drew McGowen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!