简体   繁体   English

为什么 Clang 12 拒绝以 C++20 的方式初始化聚合?

[英]Why does Clang 12 refuse to initialize aggregates in the C++20 way?

As far as I understand, the following program should work in C++20 mode:据我了解,以下程序应该在 C++20 模式下工作:

#include <vector>

struct B{ int a0, a1; };

int main()
{
    std::vector<B> bs;
    bs.emplace_back( 0, 0 );
}

And it really does in Visual Studio 2019 and gcc 11. But not in clang 12, which produces the error:它在 Visual Studio 2019 和 gcc 11 中确实如此。但不是在 clang 12 中,这会产生错误:

/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/12.0.0/../../../../include/c++/12.0.0/bits/alloc_traits.h:514:4: error: no matching function for call to 'construct_at'
          std::construct_at(__p, std::forward<_Args>(__args)...);
          ^~~~~~~~~~~~~~~~~

In online compiler: https://gcc.godbolt.org/z/GzccTWc5z在在线编译器中: https : //gcc.godbolt.org/z/GzccTWc5z

Is this because clang does not fully support C++20 yet?这是因为 clang 还没有完全支持 C++20 吗?

This is a C++20 feature that allows aggregate initialization through the standard constructor syntax, rather than the typical braced-list initialization syntax.这是一个 C++20 特性,允许通过标准构造函数语法进行聚合初始化,而不是典型的花括号列表初始化语法。 (Note that this only works if the parameters cannot be used in a valid call to a default or copy/move constructor. If they could, that would be called instead of performing aggregate initialization.) (请注意,这仅适用于无法在对默认或复制/移动构造函数的有效调用中使用参数的情况。如果可以,则将调用该函数而不是执行聚合初始化。)

According to the official C++ Support in Clang page, Clang does not yet support parenthesized initialization of aggregates (which is P0960R3 and P1975R0 ).根据Clang 中的官方C++ 支持页面,Clang 尚不支持聚合的括号初始化(即P0960R3P1975R0 )。 This is as of Clang version 13.这是从 Clang 版本 13 开始的。

A support matrix for C++20 features is also maintained on cppreference.com . cppreference.com 上还维护了 C++20 特性的支持矩阵。 This shows that support for P0960R3 is as follows:这表明对 P0960R3 的支持如下:

  • GCC: supported as of version 10 GCC:从版本 10 开始支持
  • MSVC: supported as of version 19.28 MSVC:从 19.28 版开始支持
  • EDG eccp: supported as of version 5.1 EDG eccp:从 5.1 版开始支持
  • Clang and Apple Clang: unsupported Clang 和 Apple Clang:不受支持
  • Intel ICC: unsupported英特尔 ICC:不支持

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM