Brace elision in std::array<std::vector>

  • A+

I'm compiling using g++ for C++ 17. I have the following:

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

I don't understand why if I remove the double braces for the array it does not work anymore.

std::array<std::vector<int>, 2> v = { {1,2}, {3,4} }; // Does not compile 

I understand how std::array works and the need for the double braces in general, but as I'm compiling for C++17 I expected brace elision to come into play.

Why is brace elision not applicable here?


As T.C. pointed out my original interpretation was not corret, brace elision is allowed see [dcl.init.aggr]p15:

Braces can be elided in an initializer-list as follows. If the initializer-list begins with a left brace, then the succeeding comma-separated list of initializer-clauses initializes the elements of a subaggregate; it is erroneous for there to be more initializer-clauses than elements. If, however, the initializer-list for a subaggregate does not begin with a left brace, then only enough initializer-clauses from the list are taken to initialize the elements of the subaggregate; any remaining initializer-clauses are left to initialize the next element of the aggregate of which the current subaggregate is an element. ...

but std::array according to array.overview:

An array is an aggregate that can be list-initialized with up to N elements whose types are convertible to T.

which is not the case we have.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: