【问题标题】:Compiler should raise an error for certain combinations of #define编译器应该为#define 的某些组合引发错误
【发布时间】:2012-07-08 10:13:24
【问题描述】:
在当前的项目中,我进行了很多试验,以了解不同解决方案对性能的影响。因为我喜欢保留所有代码,所以我有很多#ifdef 指令,这使我可以轻松地打开和关闭一些优化。但是,某些定义组合并未涵盖。如果发生这种情况,我希望看到编译器错误,即:
#define A
#define B
#ifdef A
#ifdef B
//invalid combination of defines. Compiler should raise an error.
#endif
#endif
#ifdef A
//do something
#endif
#ifdef B
//do something else
#endif
这可能吗?
【问题讨论】:
标签:
c-preprocessor
preprocessor-directive
【解决方案1】:
是的。只需使用error directive (#error)。
#ifdef A
#ifdef B
#error "invalid combination of defines."
#endif
#endif
【解决方案2】:
#ifdef A
#ifdef B
//invalid combination of defines. Compiler should raise an error.
#error Invalid combination
#endif
#endif
【解决方案3】:
使用错误预处理器指令:
#error "Invalid combination"
【解决方案4】:
#if defined(A) && defined(B)
#error invalid combination of defines
#endif