by elteto · 7y ago · 0 comments
Say I have a static list of names and I would like to declare some struct type for each name. I also would like to create variables of these structs at some point, and I would always do so for the entire block of names. You could do something like this:
#define apply(fn) \
fn(name1) \
fn(name2) \
fn(name3) \
...
fn(nameN)
#define make_struct(name) struct name##_t { ... }
#define make_variable_of(name) name##_t name;
...
apply(make_struct); // This defines all the structs.
void some_function(...) {
apply(make_variable_of); // And this defines one variable of each type.
}
Yes, it is not pretty (it is the C preprocessor after all), but it can be very useful and clean.I used this trick to declare and generate code for an entire parser, it's lovely.
I recently switched to the other style of C-macro though:
#define X(A, B, C) ...
#include <math/apply_operators.h>
#undef X
This style is less powerful but doesn't require the annoying \ at the end of lines. enum foo {
FOO_THING_ONE,
FOO_THING_TWO,
FOO_THING_THREE,
...
FOO_THING_SEVEN_HUNDRED
};
// Using concatenate '##' and stringify '#' operators
#define FANCYCASE(X) case FOO_THING_##X: str=#X; break
const char *foo_to_str(enum foo myFoo)
{
char *str;
switch(myFoo)
{
FANCYCASE(ONE);
FANCYCASE(TWO);
FANCYCASE(THREE);
...
FANCYCASE(SEVEN_HUNDRED);
}
return str;
}