【问题标题】:Why does this clang code fail to compile with clang 10 with -std=c++20为什么此 clang 代码无法使用带有 -std=c++20 的 clang 10 编译
【发布时间】:2026-02-04 18:00:01
【问题描述】:

以下程序使用 clang10 和 -std=c++20 编译失败

#include "clang/AST/ASTContext.h"
int main(){}

使用 -std=c++17 即可。

这是编译尝试的输出(请注意,我对 C++17 中的链接器错误很好,因为我没有将所需的 -l 提供给命令行)

clang++-10  toy.cc -I/usr/lib/llvm-10/include -std=c++20 -w
In file included from toy.cc:1:
In file included from /usr/lib/llvm-10/include/clang/AST/ASTContext.h:28:
In file included from /usr/lib/llvm-10/include/clang/AST/RawCommentList.h:14:
/usr/lib/llvm-10/include/clang/Basic/SourceManager.h:953:59: error: use of overloaded operator '!=' is ambiguous (with operand types 'llvm::DenseMapBase<llvm::DenseMap<const clang::FileEntry *, const clang::FileEntry *, llvm::DenseMapInfo<const clang::FileEntry *>, llvm::detail::DenseMapPair<const clang::FileEntry *, const clang::FileEntry *> >, const clang::FileEntry *, const clang::FileEntry *, llvm::DenseMapInfo<const clang::FileEntry *>, llvm::detail::DenseMapPair<const clang::FileEntry *, const clang::FileEntry *> >::iterator' (aka 'DenseMapIterator<const clang::FileEntry *, const clang::FileEntry *, llvm::DenseMapInfo<const clang::FileEntry *>, llvm::detail::DenseMapPair<const clang::FileEntry *, const clang::FileEntry *> >') and 'llvm::DenseMapBase<llvm::DenseMap<const clang::FileEntry *, const clang::FileEntry *, llvm::DenseMapInfo<const clang::FileEntry *>, llvm::detail::DenseMapPair<const clang::FileEntry *, const clang::FileEntry *> >, const clang::FileEntry *, const clang::FileEntry *, llvm::DenseMapInfo<const clang::FileEntry *>, llvm::detail::DenseMapPair<const clang::FileEntry *, const clang::FileEntry *> >::iterator')
      if (OverriddenFilesInfo->OverriddenFiles.find(File) !=
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
/usr/lib/llvm-10/include/llvm/ADT/DenseMap.h:1222:8: note: candidate function
  bool operator!=(const ConstIterator &RHS) const {
       ^
/usr/lib/llvm-10/include/llvm/ADT/DenseMap.h:1215:8: note: candidate function
  bool operator==(const ConstIterator &RHS) const {
       ^
/usr/lib/llvm-10/include/llvm/ADT/DenseMap.h:1215:8: note: candidate function (with reversed parameter order)
1 error generated.
clang++-10  toy.cc -I/usr/lib/llvm-10/include -std=c++17 -w
/usr/bin/ld: /tmp/toy-4396eb.o:(.data+0x0): undefined reference to `llvm::DisableABIBreakingChecks'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

注意事项:

  • 标记了这艘宇宙飞船,因为我不知道与 != == C++20 中的变化

  • 不能减少这个例子,因为 DenseMap 是一个类的怪物, 我发现了类似的问题,解决方案是运营商是 缺少一个 const 限定符,这似乎不是问题(我 可以在源代码中看到 const ),当我试图获得类似的 一个简单案例的错误我没有得到一个错误。

【问题讨论】:

    标签: c++ c++20 spaceship-operator


    【解决方案1】:

    这个 LLVM 示例简化为:

    struct iterator;
    
    struct const_iterator {
        const_iterator(iterator const&);
    };
    
    struct iterator {
        bool operator==(const_iterator const&) const;
        bool operator!=(const_iterator const&) const;
    };
    
    bool b = iterator{} != iterator{};
    

    在 C++17 中,这很好:我们只有一个候选者并且它是可行的(iterator 可以转换为 const_iterator,所以一个可以工作)。

    在 C++20 中,我们突然有了三个候选项。我将使用非成员语法将它们写出来,以便参数更明显:

    bool operator==(iterator const&, const_iterator const&); // #1
    bool operator==(const_iterator const&, iterator const&); // #2 (reversed #1)
    bool operator!=(iterator const&, const_iterator const&); // #3
    

    #2#1 的反向候选。 #3 没有反向候选,因为只有主要比较运算符(==&lt;=&gt;)得到反向候选。

    现在,重载解析的第一步是执行转换序列。我们有两个iterator 类型的参数:对于#1,这是一个完全匹配/转换。对于#2,这是转换/精确匹配。对于#3,这是完全匹配/转换。这里的问题是我们在#1#2 之间存在这种“触发器”:在一个参数/参数对中每个都更好,而在另一个参数/参数对中则更差。这是模棱两可的。即使#3 在某种意义上是“更好的候选者”,我们也没有得到那么远 - 模棱两可的转换序列意味着模棱两可的重载解决方案。


    现在,gcc 编译这个 反正(我不完全确定它在这里实现了哪些具体规则)甚至 clang 甚至都不认为这是一个错误,只是一个警告(你可以禁用它与-Wno-ambiguous-reversed-operator)。有一些正在进行的工作试图更优雅地解决这些情况。


    为了更有帮助,这里是 LLVM 示例的更直接简化,以及我们如何为 C++20 修复它:

    template <bool Const>
    struct iterator {
        using const_iterator = iterator<true>;
    
        iterator();
    
        template <bool B, std::enable_if_t<(Const && !B), int> = 0>
        iterator(iterator<B> const&);
    
    #if __cpp_impl_three_way_comparison >= 201902
        bool operator==(iterator const&) const;
    #else
        bool operator==(const_iterator const&) const;
        bool operator!=(const_iterator const&) const;
    #endif
    };
    

    在 C++20 中,我们需要一个齐次比较运算符。

    这在 C++17 中不起作用,因为要支持 iterator&lt;false&gt;{} == iterator&lt;true&gt;{} 案例:唯一的候选者是 iterator&lt;false&gt;::operator==(iterator&lt;false&gt;),您不能将 const_iterator 转换为 iterator

    但在 C++20 中没问题,因为在这种情况下,现在我们有 两个 候选:iterator&lt;false&gt; 的相等运算符 iterator&lt;true&gt; 的反转相等运算符。前者是不可行的,但后者是并且工作正常。

    我们也只需要operator==operator!= 我们只是免费获得的,因为我们想要的只是否定的平等。

    或者,您可以在 C++17 中将比较运算符编写为隐藏的朋友:

    friend bool operator==(iterator const&, iterator const&);
    friend bool operator!=(iterator const&, iterator const&);
    

    通过让两种类型的候选人参与,这可以让您获得与 C++20 相同的行为(与 C++20 版本相比,它只需要编写一个额外的函数,并且该函数必须是隐藏的朋友 - 它不能成为会员)。

    【讨论】:

    • 很好的答案,唯一的问题是我认为我用 -w 禁用了所有警告,但我想这只是我误解了编译器选项。这似乎也是 Richard Smith/WG21 所熟悉的:mail-archive.com/…
    • @NoSenseEtAl 是的,Richard Smith 对一切都很熟悉 :-)。我无法完全从 OP 中重现该问题,但我继续提交了 review 来修复它。
    • 温和的题外话,但你仍然喜欢免费的 == 函数而不是成员函数吗?我认为 foonathan 建议让他们成为会员,这样当你搞砸时,你在错误消息中收到的候选人更少。如果这太离题了,我会提出另一个问题。
    • 为后代:合并请求与扩展为reviews.llvm.org/D78938的示例
    • "甚至clang都不认为这是一个错误,只是一个警告"实际上是错误,选项没有帮助:godbolt.org/z/jWYGdd
    最近更新 更多