为什么std::vector和std::array的c++ initializer_list行为不同?

时间:2021-01-10 21:21:57

Code:

代码:

std::vector<int> x{1,2,3,4};
std::array<int, 4> y{{1,2,3,4}};

Why do I need double curly braces for std::array?

为什么std::array需要双花括号?

2 个解决方案

#1


55  

std::array<T, N> is an aggregate: it doesn't have any user-declared constructors, not even one taking a std::initializer_list. Initialization using braces is performed using aggregate initialization, a feature of C++ that was inherited from C.

数组 是一个集合:它没有任何用户声明的构造函数,甚至没有一个使用std::initializer_list。使用大括号进行初始化使用聚合初始化,这是c++从C继承的特性。 ,>

The "old style" of aggregate initialization uses the =:

集合初始化的“旧样式”使用=:

std::array<int, 4> y = { { 1, 2, 3, 4 } };

With this old style of aggregate initialization, extra braces may be elided, so this is equivalent to:

使用这种老式的聚合初始化方式,可以省略额外的大括号,因此这相当于:

std::array<int, 4> y = { 1, 2, 3, 4 };

However, these extra braces may only be elided "in a declaration of the form T x = { a };" (C++11 §8.5.1/11), that is, when the old style = is used . This rule allowing brace elision does not apply for direct list initialization. A footnote here reads: "Braces cannot be elided in other uses of list-initialization."

但是,这些额外的大括号只能在“T x = {a}的声明中”省略;(c++ 11§8.5.1/11),也就是说,当使用旧式=。允许省略括号的规则不适用于直接的列表初始化。这里的脚注是:“在列表初始化的其他用途中,不能省略括号。”

There is a defect report concerning this restriction: CWG defect #1270. If the proposed resolution is adopted, brace elision will be allowed for other forms of list initialization, and the following will be well-formed:

关于这个限制有一个缺陷报告:CWG缺陷#1270。如果采用建议的决议,则允许其他形式的列表初始化使用括号省略,格式良好:

std::array<int, 4> y{ 1, 2, 3, 4 };

(Hat tip to Ville Voutilainen for finding the defect report.)

(这是找到缺陷报告的给维蒂莱宁的小费。)

#2


23  

Because std::vector offers a constructor that takes in a std::initializer_list<T>, while std::array has no constructors and the {1, 2, 3, 4} braced init-list is in fact not interpreted as a std::initializer_list, but aggregate initialization for the inner C-style array of std::array (that's where the second set of braces comes from: One for std::array, one for the inner C-style member array).

因为std::向量提供一个构造函数,该函数接收std::initializer_list < T >,而std::数组没有构造函数,{ 1,2,3,4 }做好init-list事实上不是解释为std::initializer_list,但总初始化数组内c风格的std::数组(这是第二组括号从哪里来:一个用于std::数组,一个内心的c风格的成员数组)。

#1


55  

std::array<T, N> is an aggregate: it doesn't have any user-declared constructors, not even one taking a std::initializer_list. Initialization using braces is performed using aggregate initialization, a feature of C++ that was inherited from C.

数组 是一个集合:它没有任何用户声明的构造函数,甚至没有一个使用std::initializer_list。使用大括号进行初始化使用聚合初始化,这是c++从C继承的特性。 ,>

The "old style" of aggregate initialization uses the =:

集合初始化的“旧样式”使用=:

std::array<int, 4> y = { { 1, 2, 3, 4 } };

With this old style of aggregate initialization, extra braces may be elided, so this is equivalent to:

使用这种老式的聚合初始化方式,可以省略额外的大括号,因此这相当于:

std::array<int, 4> y = { 1, 2, 3, 4 };

However, these extra braces may only be elided "in a declaration of the form T x = { a };" (C++11 §8.5.1/11), that is, when the old style = is used . This rule allowing brace elision does not apply for direct list initialization. A footnote here reads: "Braces cannot be elided in other uses of list-initialization."

但是,这些额外的大括号只能在“T x = {a}的声明中”省略;(c++ 11§8.5.1/11),也就是说,当使用旧式=。允许省略括号的规则不适用于直接的列表初始化。这里的脚注是:“在列表初始化的其他用途中,不能省略括号。”

There is a defect report concerning this restriction: CWG defect #1270. If the proposed resolution is adopted, brace elision will be allowed for other forms of list initialization, and the following will be well-formed:

关于这个限制有一个缺陷报告:CWG缺陷#1270。如果采用建议的决议,则允许其他形式的列表初始化使用括号省略,格式良好:

std::array<int, 4> y{ 1, 2, 3, 4 };

(Hat tip to Ville Voutilainen for finding the defect report.)

(这是找到缺陷报告的给维蒂莱宁的小费。)

#2


23  

Because std::vector offers a constructor that takes in a std::initializer_list<T>, while std::array has no constructors and the {1, 2, 3, 4} braced init-list is in fact not interpreted as a std::initializer_list, but aggregate initialization for the inner C-style array of std::array (that's where the second set of braces comes from: One for std::array, one for the inner C-style member array).

因为std::向量提供一个构造函数,该函数接收std::initializer_list < T >,而std::数组没有构造函数,{ 1,2,3,4 }做好init-list事实上不是解释为std::initializer_list,但总初始化数组内c风格的std::数组(这是第二组括号从哪里来:一个用于std::数组,一个内心的c风格的成员数组)。