如何在GCC13中消除模糊反转运算符警告(第三方代码不可修改)
GCC13消除C++20反向运算符歧义警告的方法
问题场景:无法修改的第三方代码在GCC13编译时触发如下警告:
warning: C++20 says that these are ambiguous, even though the second is reversed
Clang中可通过以下编译指令消除该警告:
#pragma GCC diagnostic ignored "-Wambiguous-reversed-operator"GCC13中同样使用该编译指令即可消除警告,修改后的示例代码如下:
// 触发警告的示例代码 template <typename> struct S { friend bool operator==(S&&, const S&) { return true; } friend bool operator!=(S&&, const S&) { return true; } }; using U = S<int>; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wambiguous-reversed-operator" bool b = U{} == U{}; #pragma GCC diagnostic pop
该警告在GCC13中由-Wambiguous-reversed-operator选项控制,该选项在C++20模式下默认启用,通过上述#pragma指令可临时屏蔽该警告,同时不影响其他代码的警告检查。
内容的提问来源于stack exchange,提问作者Mikhail
相关产品推荐
相关产品推荐

