Possible Duplicate:
C, Macro defining Macro
Does anyone know how to pull off something like this…
I have a lot of repetitive macros as : –
#define MYMACRO1(x) Do1(x)
#define MYMACRO2(x,y) Do2(x, y)
#define MYNEXTMACRO1(x) Do1(x)
#define MYNEXTMACRO2(x,y) Do2(x, y)
The code above works fine, but I want to write a macro that creates macros (a meta macro).
For example: –
#define MYMETAMACRO(name) \
#define #name1(x) Do1(x) \
#define #name2(x,y) Do2(x, y) \
Such that I can do : –
MYMETAMACRO(MYMACRO);
MYMETAMACRO(MYNEXTMACRO);
and then : –
MYMACRO1(2);
MYMACRO2(2,3);
MYNEXTMACRO1(4);
MYNEXTMACRO2(4, 5);
The preprocessor bombs out at the #define as it thinks it is a missing parameter of the macro.
As far as I know, you can’t write a macros that writes another macros in C/C++.
I’ve spent enough time trying to do something like that using C preprocessor in C++ in the past, asked people around, and concluded that it isn’t possible.