问题
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?
回答1:
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".
来源:https://stackoverflow.com/questions/25331999/macro-meta-programming