std::array<T, N> is a collection: it does not have any user-declared constructors, even one of them does not accept std::initializer_list . Initialization using curly braces is performed using aggregate initialization, a C ++ function that was inherited from C.
In the old style, aggregate initialization uses = :
std::array<int, 4> y = { { 1, 2, 3, 4 } };
In this old aggregate initialization style, extra curly braces can be undone, so this is equivalent to:
std::array<int, 4> y = { 1, 2, 3, 4 };
However, these additional curly braces can only be canceled "in the declaration of the form T x = { a }; " (C ++ 11 §8.5.1 / 11), that is, when the old style = . This rule, which allows alignment of shapes, is not used to directly initialize a list. The footnote here says: "Brackets cannot be deleted with other uses of list initialization."
There is a bug report regarding this limitation: defect CWG # 1270 . If the adopted proposed resolution is adopted, the exclusion of curly braces will be allowed for other forms of list initialization, and the following will be well-formed:
std::array<int, 4> y{ 1, 2, 3, 4 };
(Ville Voutilainen hat tip for finding a bug report.)
James McNellis Jul 09 2018-12-12T00: 00Z
source share