Why std :: array does not contain an initializer list constructor

To initialize std::array with some values, you need to use this approach:

 std::array<int,3> an_array{{3,4,5}}; 

I know that we need two curly braces (one for std::array , and the other for internal c-style array ).

My question is: Why, by standard, std::array does not contain an initializer list constructor that directly initializes the internal c-style array ? Not more comfortable for the eyes to be initialized as:

 std::array<int,3> an_array{3,4,5}; 

Edit:

This information is from http://en.cppreference.com/w/cpp/container/array . I thought that my compiler resolves the second version directly as a non-standard extension. Now I'm not even sure what the standard is in this case.

// construct uses aggregate initialization

std::array<int, 3> a1{ {1, 2, 3} }; // double brackets required in C ++ 11 (not in C ++ 14)

+7
c ++ arrays c ++ 11
source share
1 answer

The standard defines std::array as follows (N3337 for C ++ 11, but the parts quoted are identical in N4140):

ยง23.3.2.1 [array.overview] / 2

An array is a collection that can be initialized with syntax

 array<T, N> a = { initializer-list }; 

and the population is defined as:

ยง8.5.1 [dcl.init.aggr] / 1

An aggregate is an array or class that does not contain user constructors, there are no private or protected non-static data elements, there is no base of classes and virtual functions.

Therefore, it cannot have a user-defined constructor that would be initializer_list .


In addition, C ++ 11 defines alignment of shapes only for the syntax T x = { a } :

ยง8.5.1 [dcl.init.aggr] / 11

In the form declaration

 T x = { a }; 
Parentheses

can be dropped in the initializer list as follows. [...]

whereas C ++ 14 (N4140) cancels this requirement:

ยง8.5.1 [dcl.init.aggr] / 11

Braces can be discarded in the initializer list as follows. [...]

So, C ++ 14 and above is absolutely true:

 std::array<int,3> an_array{3,4,5} 
+6
source share

All Articles