简体   繁体   English

在 c++17 中编译的简单代码在 c++20 中产生错误

[英]simple code that compile in c++17 produce error with c++20

I have a strange error returned by gcc/clang When I switch from std=c++17 to std=c++20.当我从 std=c++17 切换到 std=c++20 时,gcc/clang 返回了一个奇怪的错误。

struct Matrix2 {
   double ptr[9];

   // defaults
   Matrix2()                          = default; // constructor
   Matrix2(const Matrix2&)            = default; // copy constructor
   Matrix2(Matrix2&&)                 = default; // move constructor
   Matrix2& operator=(const Matrix2&) = default; // copy assignment operator
   Matrix2& operator=(Matrix2&&)      = default; // move assignment operator
   ~Matrix2()                         = default; // destructor

};

constexpr Matrix2 Id2() {
    return {   1.0   ,   0.0   ,   0.0   ,
               0.0   ,   1.0   ,   0.0   ,
               0.0   ,   0.0   ,   1.0   };
}

int main () {
   auto a = Id2();
}

with stdc++17, the code compile fine, but with stdc++20 this produce the following error: could not convert '{1.0e+0, 0.0, 0.0, 0.0, 1.0e+0, 0.0, 0.0, 0.0, 1.0e+0}' from '<brace-enclosed initializer list>' to 'Matrix2'使用 stdc++17,代码编译良好,但使用 stdc++20 会产生以下错误: could not convert '{1.0e+0, 0.0, 0.0, 0.0, 1.0e+0, 0.0, 0.0, 0.0, 1.0e+0}' from '<brace-enclosed initializer list>' to 'Matrix2'

https://godbolt.org/z/P4afYYn9d https://godbolt.org/z/P4afYYn9d

Does the standard now prohibit returning raw initializer_list??标准现在是否禁止返回原始初始化器列表? and what is the work around??什么是解决方法?

Thx a lot多谢

There was a change in the C++ standard between C++17 and C++20 for aggregate initialization. C++17 和 C++20 之间的 C++ 标准发生了变化,用于聚合初始化。 Have a look at aggregate initialization (cppreference)看看聚合初始化(cppreference)

Look at the explanation section:看解释部分:

  • since c++11, until c++20 : no user-provided, inherited, or explicit constructors (explicitly defaulted or deleted constructors are allowed)自 c++11 起,直到 c++20 :没有用户提供的、继承的或显式的构造函数(允许显式默认或删除的构造函数)
  • since C++20 : no user-declared or inherited constructors自 C++20 起:没有用户声明或继承的构造函数

You have declared constructors in your class/struct, so as of C++20 you can't have this kind of initialization.您已经在类/结构中声明了构造函数,因此从 C++20 开始,您不能进行这种初始化。 You'd have to declare your own constructor with initialization list您必须使用初始化列表声明自己的构造函数

[edit: adding some detailed examples] [编辑:添加一些详细示例]

Or remove all the defaulted constructors.或者删除所有默认的构造函数。

You could also have a constructor from std::array :你也可以有一个std::array的构造函数:

struct Matrix2 {
   std::array<double,9> ptr;

   // defaults
   Matrix2()                          = default; // constructor
   Matrix2(const Matrix2&)            = default; // copy constructor
   Matrix2(Matrix2&&)                 = default; // move constructor
   Matrix2& operator=(const Matrix2&) = default; // copy assignment operator
   Matrix2& operator=(Matrix2&&)      = default; // move assignment operator
   constexpr Matrix2(const std::array<double, 9> & arr)
   : ptr(arr) {}
   ~Matrix2()                         = default; // destructor    
};

constexpr Matrix2 Id2() {
    // not nice, requires extra brace.
    // not nice, potentially slower if the compiler misses
    // the opportunity to elide the copy of the array.
    return {{   1.0   ,   0.0   ,   0.0   ,
               0.0   ,   1.0   ,   0.0   ,
               0.0   ,   0.0   ,   1.0   }};
}

int main () {
   auto a = Id2();
}

Or with initialization list:或使用初始化列表:

struct Matrix2 {
   double ptr[9];

   // defaults
   Matrix2()                          = default; // constructor
   Matrix2(const Matrix2&)            = default; // copy constructor
   Matrix2(Matrix2&&)                 = default; // move constructor
   Matrix2& operator=(const Matrix2&) = default; // copy assignment operator
   Matrix2& operator=(Matrix2&&)      = default; // move assignment operator
   constexpr Matrix2(const std::initializer_list<double> & init)
   :ptr{}
   {
       // not nice, this is not as good as the validation 
       // the compiler does for aggregate initialization.
       assert(std::size(init) <= std::size(ptr));
       std::copy(std::begin(init), std::end(init), ptr);
   }
   ~Matrix2()                         = default; // destructor

};

constexpr Matrix2 Id2() {
    return {   1.0   ,   0.0   ,   0.0   ,
               0.0   ,   1.0   ,   0.0   ,
               0.0   ,   0.0   ,   1.0   };
}

int main () {
   auto a = Id2();
}

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

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