Macros's that define macros

Posted by David Thornley on Stack Overflow See other posts from Stack Overflow or by David Thornley
Published on 2010-05-27T02:02:35Z Indexed on 2010/05/27 2:11 UTC
Read the original article Hit count: 534

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about c

    Related posts about preprocessor