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

后端 未结 8 949
孤独总比滥情好
孤独总比滥情好 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:20

    If DO_THIS is yours definition then simple and working solution seems to be usage of Function-like Macro:

    #define DO_THIS() 1
    
    #if DO_THIS()
        //stuff
    #endif
    

    I tested this under Visual Studio 2008, 2015 and GCC v7.1.1. If DO_THIS() is undefined VS gererates:

    warning C4067: unexpected tokens following preprocessor directive - expected a newline

    and GCC generates

    error: missing binary operator before token "("

提交回复
热议问题