[英]initializer_list in initialization list
I want initialize base class with std::initializer_list
.我想用std::initializer_list
初始化基类。
struct A : public std::array<int, 4>
{
// This constructor works fine
A()
: std::array<int, 4>{{ 1, 2, 3, 4 }}
{
}
// THIS CONSTRUCTOR FAILS TO COMPILE
A(std::initializer_list<int> il)
: std::array<int, 4>{il}
{
}
};
GCC error for second constructor is第二个构造函数的 GCC 错误是
error: array must be initialized with a brace-enclosed initializer错误:数组必须用花括号括起来的初始化器初始化
What I want is to initialize new A instance with initializer_list like this我想要的是像这样用 initializer_list 初始化新的 A 实例
A var{{ 1, 2, 3, 4 }}
and pass it to base class.并将其传递给基类。
The class std::array
has no constructor taking a std::initializer_list
.类std::array
没有采用std::initializer_list
构造std::initializer_list
。
The only way you have is to do it like this :你唯一的方法就是这样做:
#include <array>
#include <initializer_list>
struct A : public std::array<int, 4>
{
A()
: std::array<int, 4>{{ 1, 2, 3, 4 }}
{
}
A(std::array<int, 4> il)
: std::array<int, 4>(il)
{
}
};
int main ()
{
A a ({{ 1, 2, 3, 4 }});
}
It's saying that an array
can be initialized with a "brace-enclosed initializer" as in your first ctor, but not with an initializer_list
as in your second.它是说可以像在第一个 ctor 中那样使用“大括号封闭式初始化程序”初始化array
,但不能像在第二个 ctor 中那样使用initializer_list
。 There is no array ctor that takes an initializer_list.没有采用 initializer_list 的数组构造函数。 In fact, there are no ctors for arrays except for the implicitly-declared special member functions.事实上,除了隐式声明的特殊成员函数之外,没有用于数组的构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.