Why no warning with “#if X” when X undefined?

后端 未结 8 917
孤独总比滥情好
孤独总比滥情好 2021-02-18 13:06

I occasionally write code something like this:

// file1.cpp
#define DO_THIS 1

#if DO_THIS
    // stuff
#endif

During the code development I ma

相关标签:
8条回答
  • 2021-02-18 13:32

    If you're desperate to prevent this kind of error, try the following which uses preprocessor token-pasting magic and expression evaluation to enforce that a macro is defined (and 0 in this example):

    #define DEFINED_VALUE(x,y) (defined (y##x) ? x : 1/x)
    #if DEFINED_VALUE(FEATURE1,) == 0
    
    0 讨论(0)
  • 2021-02-18 13:39

    gcc can generate a warning for this, but its probably not required by the standard:

    -Wundef
    Warn if an undefined identifier is evaluated in an `#if' directive.

    0 讨论(0)
提交回复
热议问题