tags:

views:

87

answers:

2

Possible Duplicate:
C, Macro defining Macro

Does anyone know how to pull off something like this...

I have alot 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.

+1  A: 

You cannot define macros in other macros, but you can call a macro from your macro, which can get you essentially the same results.

#define B(x) do {printf("%d", (x)) }while(0)
#define A(x) B(x)

so, A(y) is expanded to do {printf("%d", (y)) }while(0)

WhirlWind
+1  A: 

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.

SigTerm