1

在当前的项目中,我进行了很多试验,以了解不同解决方案对性能的影响。因为我喜欢保留所有代码,所以我有很多#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

那可能吗?

4

4 回答 4

8

是的。只需使用错误指令( #error)。

#ifdef A
#ifdef B
#error "invalid combination of defines."
#endif
#endif
于 2012-07-08T10:15:31.373 回答
2
#ifdef A
#ifdef B
//invalid combination of defines. Compiler should raise an error.
#error Invalid combination
#endif
#endif
于 2012-07-08T10:15:43.027 回答
2

使用错误预处理器指令:

#error "Invalid combination"
于 2012-07-08T10:18:28.847 回答
2
#if defined(A) && defined(B)
#error invalid combination of defines
#endif
于 2012-07-08T10:19:22.397 回答