X-Macros

This blew my mind and made my eyes bleed at the same time. My convoluted version of X-Macros:

char* foo() { return "foo"; }
char* bar() { return "bar"; }
void baz(char *z, char* a, char* b) { printf("BAZ: %s, %s\n", a, b); }

#define ARGS(_) ,_

#define KERNEL_CALL(a) \
KERNEL_ARGS(PRINTF_MACRO); \
baz("boo" KERNEL_ARGS(ARGS)); \
printf("Now we print %s\n", a);
int main() {
#define KERNEL_ARGS(_) _(foo()) _(bar())
#define PRINTF_MACRO(_) printf("I am printing %s\n",_);
KERNEL_CALL("done");
#undef KERNEL_ARGS
#undef PRINTF_MACRO
}

The biggest problem with this approach is the opaqueness it introduces into your source. You can’t grep for the results of the preprocessor, so if you use X-macros to create functions by concatenating arguments together (which is exactly what they’re good at) then grepping turns up nothing!

Comments are closed.