C++ 如何处理叮当声';s(3.9)-扩展到定义的警告?

C++ 如何处理叮当声';s(3.9)-扩展到定义的警告?,c++,c,clang,undefined-behavior,defined,C++,C,Clang,Undefined Behavior,Defined,clang 3.9在-Walla中添加了警告-weexpansion to defined,该警告会产生 生成“已定义”的宏扩展具有未定义的行为 在定义的在#if表达式之外使用,包括在#if表达式中使用的宏的情况。例如,下面的代码 // in some file: #define HAS_GNU (defined(__GNUC__) && !defined(__clang__)) // possibly in another file: #if defined(__clang_

clang 3.9在
-Wall
a中添加了警告
-weexpansion to 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__))
那么“正确”的方法是什么呢?

您可以使用#if-#else宏:

#if defined(__GNUC__) && !defined(__clang__)
#define HAS_GNU 1
#else
#define HAS_GNU 0
#endif
或者,如果您愿意更改使用
HAS_GNU
的代码,则可能采用更传统的方式:

#if defined(__GNUC__) && !defined(__clang__)
#define HAS_GNU
#endif

#if defined(__clang__) || defined(HAS_GNU)

如果你在3d派对吊舱上遇到这种问题,你可能会发现这很有用

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wexpansion-to-defined"
#import <pop/POP.h>
#pragma clang diagnostic pop
#pragma-clang诊断推送
#pragma-clang诊断被忽略“-weexpansion to-defined”
#进口
#pragma-clang诊断流行语

这并不能回答问题。问题不是禁用警告。问题是“如何处理…”。如果您有第三个库,那么显然无法对源代码进行更改。这就是如何“处理”这个警告,尤其是当项目将警告视为错误时