c - 宏扩展 : should it work? 后重新扫描 'defined' 运算符

标签 c macros doxygen c-preprocessor

考虑

#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/

相关文章:

c++ - 弃用 C++ 中类的旧名称

c++ - doxygen 中引用运算符的问题

ubuntu - 在 wsl 上安装 doxygen 无法找到 Iconv(缺少 : ICONV_COMPILES)

c - 有没有比在 C 中使用 rb_funcall() 更惯用的方法来初始化 Ruby 对象?

c - scanf 函数打印出无关紧要的内容

c - 为什么这段代码不计算负数的立方根?

java - 使用 Z3 的 Java 中的段错误

macros - Rust 宏可以处理多层嵌套表达式吗?

c - 可变参数在 CC 编译器上的 AIX 5.3 中不起作用

c++ - Doxygen 如何排除 C++ 类?