的std ::阵列< T>初始化

的std ::阵列< T>初始化

问题描述:

A 的std ::阵列< T> 本质上是一个C数组包裹在一个结构结构 S的初始化需要大括号和数组的初始化需要大括号为好。所以,我需要2对括号:

A std::array<T> is essentially a C-style array wrapped in a struct. The initialization of structs requires braces, and the initialization of arrays requires braces as well. So I need two pairs of braces:

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

但大多数的例子code,我只看到了使用一对大括号:

But most of the example code I have seen only uses one pair of braces:

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

如何走到这是允许的,并且相比第一计算策略它有什么好处还是缺点?

How come this is allowed, and does it have any benefits or drawbacks compared to the first approch?

这样做的好处是,你必须......打字比较少。但缺点是,你只被允许离开过牙套时声明有这种形式。如果你离开关闭 = ,或者如果数组成员,并与初始化成员{{1,2,3,4,5 }} ,你不仅可以通过一对大括号。

The benefit is that you have ... less to type. But the drawback is that you are only allowed to leave off braces when the declaration has that form. If you leave off the =, or if the array is a member and you initialize it with member{{1, 2, 3, 4, 5}}, you cannot only pass one pair of braces.

这是因为,当括号中的传递给函数为f({{1,2,3,4,5}})是可能的超载歧义的忧虑>。但它引起的已经产生了一些讨论和问题报告。

This is because there were worries of possible overload ambiguities when braces are passed to functions, as in f({{1, 2, 3, 4, 5}}). But it caused some discussion and an issue report has been generated.

从本质上讲, = {...} 初始化一直是能够

Essentially, the = { ... } initialization always has been able to omit braces, as in

int a[][2] = { 1, 2, 3, 4 };

这不是新的。什么是新的是,你可以省略 = ,但你必须指定所有括号

That's not new. What is new is that you can omit the =, but then you must specify all braces

int a[][2]{ {1, 2}, {3, 4} };