Why can't std::array<std::pair<int,int>, 3> be initialized using nested initializer lists, but std::vector<std::pair<int,int>> can?

You need to add an outer pair of braces to initialize the std::array<...> object itself:

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

The outermost pair is for the array object, the second pair is for the aggregate array inside the object. Then the list of elements in the array.


Specializations of the class template std::array represent aggregates that enclose another aggregate.

The reason of the error is that the first braced initializer in this declaration

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

is considered as an initializer of the inner aggregate. In this case the following braced initializers are considered as redundant initializers.

So you can write either

std::array <std::pair<int, int>, 3> a{ { {1,2},{3,4},{5,6} } };
                                       ^                   ^
                                       |the inner aggregate|

or like

std::array <std::pair<int, int>, 3> a{ std::make_pair( 1, 2 ), std::make_pair( 3, 4 ), std::make_pair( 5, 6 ) };