You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 04:24:53