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

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

    There is recursive issue. In case you have

    #define MODEL  MODEL_A
    
    #if (MODEL == MODEL_B)
     // Surprise, this is compiled!
    #endif
    

    where definition of MODEL_A and MODEL_B are missing, then it will compile.

    #ifdef MODEL
    #error Sorry, MODEL Not Defined
    // Surprise, this error is never reached (MODEL was defined by undefined symbol!)
    #endif 
    
    #ifdef MODEL_B
    #error Sorry, MODEL_B Not Defined
    // This error is reached
    #endif 
    

提交回复
热议问题