考虑
#define FOOBAR (defined(FOO) || defined(BAR))
#if FOOBAR
/* Do stuff. */
#endif
这应该有效吗?我问是因为显然我的编译器没有问题,但是 doxygen 内部预处理器认为 #if
存在语法错误。我知道我可以解决这个问题
#if defined(FOO) || defined(BAR)
#define FOOBAR 1
#endif
#if FOOBAR
/* Do stuff. */
#endif
最佳答案
来自 C99 规范:
6.10.1.3
Prior to evaluation, macro invocations in the list of preprocessing tokens that will become the controlling constant expression are replaced (except for those macro names modified by the defined unary operator), just as in normal text. If the token defined is generated as a result of this replacement process or use of the defined unary operator does not match one of the two specified forms prior to macro replacement, the behavior is undefined.
因此,如果您使用扩展为 defined
的宏,结果是未定义的。
与 C 规范中的大多数未定义事物一样,它未定义是因为标准之前的实现以不同方式处理它。
关于c - 宏扩展 : should it work? 后重新扫描 'defined' 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11904424/