Not elegantly.
Firstly, a macro cannot expand to preprocessor directives. Every directive must be visible at the toplevel without any expansion (among other things, it's syntactically impossible since there's no such thing as a multi-line macro - the \
escapes the newline, rather than including it - and directives rely on newlines as part of the syntax).
You can have conditional expansion within a macro (see here, here, etc.). But those are based on macros that expand to boolean values (or at least to some substitute for booleans). Unfortunately defined
is an operator rather than a macro, which means it can only appear in preprocessor expressions, i.e. the argument to an #if
directive. It cannot expand into anything that would be inserted into the program body, and thus cannot form part of a macro definition that relies on expansion to choose an action.
If you really want a static if macro, make t1
, t2
etc. defined at all times (the choice being between two possible values rather than between defined and undefined), and use one of the other static-if techniques in the linked questions. Or better, reconsider your need for a static if at all: in most cases, it's better to use a C-level if
statement and let the compiler optimise out the dead branch.