Perhaps try some multi-stage macro expansion? This is the strategy used by the Boost preprocessor/control/if library.
#define FOO_NAME 1
#define FOO__ 2
#define CONC(a,b) a##_##b
#define FOO(x) CONC(FOO,x)
I don't think there is any way to check conditions within a C macro expansion.
The best thing I could come up with is to covert the macro arguments to a string literal using the #
stringizing operator, and then checking using run-time functions. (This won't work for your case, though, where you want to output variable declarations.)
For example, the following prints "011":
#define FOO(x) (strcmp("NAME", #x) ? 1 : 0)
main()
{
printf("%d", FOO(NAME));
printf("%d", FOO(1));
printf("%d", FOO(2));
}
The compiler would likely optimize the strcmp
comparisons at compile-time so it would be no more inefficient than it would have been had genuine pre-processor conditionals been available. However, making FOO
a normal function would be clearer and probably just as efficient.