【发布时间】:2017-02-06 17:44:38
【问题描述】:
clang 3.9 已将警告 -Wexpansion-to-defined 添加到 -Wall,这会产生
产生“已定义”的宏扩展具有未定义的行为
如果defined 在#if 表达式之外使用,包括随后在#if 表达式中使用的宏的情况。比如下面的代码
// in some file:
#define HAS_GNU (defined(__GNUC__) && !defined(__clang__))
// possibly in another file:
#if defined(__clang__) || HAS_GNU
/* ... */
#endif
生产
test.cc:5:27: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
#if defined(__clang__) || HAS_GNU
^
test.cc:3:18: note: expanded from macro 'HAS_GNU'
#define HAS_GNU (defined(__GNUC__) && !defined(__clang__))
^
test.cc:5:27: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
test.cc:3:40: note: expanded from macro 'HAS_GNU'
#define HAS_GNU (defined(__GNUC__) && !defined(__clang__))
那么“正确”的做法是什么?
【问题讨论】:
标签: c++ c clang undefined-behavior defined