Search code examples
c++macrospreprocessorexpansion

How to expand a Boolean expression in the C++ preprocessor?


The following code does not expand the Boolean expression, see also https://godbolt.org/z/YqbazT3eo:

#define EXPAND(x) x

#define SWITCH false
EXPAND(SWITCH || defined(_DEBUG))

How do I do it correctly so that I can do

#define FLAG EXPAND(SWITCH || defined(_DEBUG))

(or similar) and FLAG will not depend on later changes to SWITCH?


Solution

  • FLAG will not depend on later changes to SWITCH?

    The only way is this:

    #if SWITCH || defined(_DEBUG)
    #define FLAG 1
    #else
    #define FLAG 0
    #endif