简体   繁体   English

为什么此 clang 代码无法使用 -std=c++20 的 clang 10 编译

[英]Why does this clang code fail to compile with clang 10 with -std=c++20

Following program fails to compile with clang10 and -std=c++20以下程序无法使用 clang10 和 -std=c++20 编译

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

With -std=c++17 it works.使用 -std=c++17 它可以工作。

This is the compile attempt output(note that I am fine with linker error in C++17 since I did not give the required -l to the command line)这是编译尝试输出(请注意,我对 C++17 中的 linker 错误很好,因为我没有向命令行提供所需的 -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)

Notes:笔记:

  • tagged this spaceship since I am not aware of the tag that relates to != == changes in C++20标记了这艘宇宙飞船,因为我不知道与 != == C++20 中的更改相关的标签

  • could not reduce this example since DenseMap is a monster of a class, I found similar question, solution there was that operators are missing a const qualifier, that does not seem to be a problem here(I can see the const in the source), and when I tried to get the similar error for a simple case I failed to get an error.无法减少此示例,因为 DenseMap 是 class 的怪物,我发现了类似的问题,解决方案是操作符缺少 const 限定符,这似乎不是问题(我可以在源代码中看到 const),当我试图在一个简单的案例中得到类似的错误时,我没有得到一个错误。

This LLVM example reduces to:这个 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{};

In C++17, this is fine: we only have one candidate and it's viable ( iterator is convertible to const_iterator so that one works).在 C++17 中,这很好:我们只有一个候选者并且它是可行的( iterator可转换为const_iterator以便一个工作)。

In C++20, we suddenly have three candidates.在 C++20 中,我们突然有了三个候选项。 I'm going to write them out using non-member syntax so the parameters are more obvious:我将使用非成员语法将它们写出来,以便参数更明显:

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 is the reversed candidate for #1 . #2#1的反向候选者。 There is no reversed candidate for #3 because only the primary comparison operators ( == and <=> ) get reversed candidates. #3没有反向候选,因为只有主要比较运算符( ==<=> )获得反向候选。

Now, the first step in overload resolution is doing conversion sequences.现在,重载解决方案的第一步是进行转换序列。 We have two arguments of type iterator : for #1 , that's an exact match/conversion.我们有两个iterator类型的 arguments :对于#1 ,这是一个完全匹配/转换。 For #2 , that's conversion/exact match.对于#2 ,这是转换/精确匹配。 For #3 , that's exact match/conversion.对于#3 ,这是完全匹配/转换。 The problem here is we have this "flip-flop" between #1 and #2 : each is better in one parameter/argument pair and worse in the other.这里的问题是我们在#1#2之间有这个“触发器”:每个参数/参数对中的每个都更好,而另一个更差。 That's ambiguous.这是模棱两可的。 Even if #3 is the "better candidate" in some sense, we don't get that far - ambiguous conversion sequence means ambiguous overload resolution.即使#3在某种意义上是“更好的候选者”,我们也没有得到那么远 - 模棱两可的转换序列意味着模棱两可的重载解决方案。


Now, gcc compiles this anyway (I'm not entirely sure what specific rules it implements here) and even clang doesn't even consider this an error, just a warning (which you can disable with -Wno-ambiguous-reversed-operator ).现在,gcc无论如何都会编译它(我不完全确定它在这里实现了哪些具体规则),甚至 clang 甚至都不认为这是一个错误,只是一个警告(你可以用-Wno-ambiguous-reversed-operator禁用它) . There's some ongoing work in trying to resolve these situations more gracefully.有一些正在进行的工作试图更优雅地解决这些情况。


To be slightly more helpful, here is a more direct reduction of the LLVM example along with how we could fix it for C++20:为了更有帮助,这里是 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
};

In C++20, we only need the one, homogeneous comparison operator.在 C++20 中,我们需要一个齐次比较运算符。

This didn't work in C++17 because of wanting to support the iterator<false>{} == iterator<true>{} case: the only candidate there is iterator<false>::operator==(iterator<false>) , and you can't convert a const_iterator to an iterator .这在 C++17 中不起作用,因为想要支持iterator<false>{} == iterator<true>{}案例:唯一的候选者是iterator<false>::operator==(iterator<false>) ,并且您不能将const_iterator转换为iterator

But in C++20 it's fine, because in this case now we have two candidates: iterator<false> 's equality operator and iterator<true> 's reversed equality operator.但是在 C++20 中这很好,因为在这种情况下,现在我们有两个候选者: iterator<false>的相等运算符iterator<true>的反向相等运算符。 The former isn't viable, but the latter is and works fine.前者是不可行的,但后者是并且工作正常。

We also only need the operator== .我们也只需要operator== The operator!= we just get for free, since all we want is negated equality. operator!=我们只是免费获得,因为我们想要的只是否定相等。

Alternatively, you could, still in C++17, write the comparison operators as hidden friends:或者,您仍然可以在 C++17 中,将比较运算符编写为隐藏的朋友:

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

Which gets you the same behavior as C++20, by way of having candidates from both types participate (it's just having to write one extra function as compared to the C++20 version, and that function must be a hidden friend - it cannot be a member).通过让两种类型的候选人参与,这可以让您获得与 C++20 相同的行为(与 C++20 版本相比,它只需要编写一个额外的 function,并且 function 必须是一个隐藏的朋友 - 它不能成为会员)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xcode 提供 -std=c++20 但命令行 clang 不提供 - Xcode offers -std=c++20 but command line clang does not C++20,如何用 Clang-10 或 GCC11 编译 - C++20, how to compile with Clang-10 or GCC11 clang++ 12 是否支持 C++20 std::construct_at? - Does clang++ 12 support C++20 std::construct_at? 编译和链接 Cuda 和 Clang 以在主机代码上支持 c++20 - Compiling and Linking Cuda and Clang to support c++20 on Host Code 为什么 Clang 12 拒绝以 C++20 的方式初始化聚合? - Why does Clang 12 refuse to initialize aggregates in the C++20 way? 为什么在使用clang -std = gnu ++ 11时编译这个C ++代码? - Why does this C++ code compile when using clang -std=gnu++11? 为什么clang ++编译代码而clang -x c ++失败? 使用std :: string进行ncurses - why clang++ compile code while clang -x c++ fails? ncurses with std::string const unordered_map &lt; std::string, int[4] &gt; 初始化失败(clang 12 / c++20) - initialise failing for const unordered_map < std::string, int[4] > (clang 12 / c++20) clang 10 C++20 概念如何指定类方法的复合要求? - How can a clang 10 C++20 concept specify compound requirements for class methods? 为什么这段代码可以用 MSVC 编译,但不能在 GCC 或 Clang 中编译? - Why does this code compile with MSVC, but not in GCC or Clang?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM