Why does initialization of array of pairs still need double braces in C++14?

This appears to be a parsing ambuguity somewhat similar to the famous most vexing parse. I suspect what's going on is that:

If you write

std::array<std::pair<int, int>, 3> b {{1, 11}, {2, 22}, {3, 33}};

the compiler has two ways to interpret the syntax:

  1. You perform a full-brace initialization (meaning the outermost brace refers to the aggregate initialization of the std::array, while the first innermost one initializes the internal member representation of std::array which is a real C-Array). This will fail to compile, as std::pair<int, int> subsequently cannot be initialized by 1 (all braces are used up). clang will give a compiler error indicating exactly that:

    error: no viable conversion from 'int' to 'std::pair<int, int>'
     std::array<std::pair<int, int>, 3> a{{1, 11}, {2, 22}, {3, 33}};
                                              ^
    

    Note also this problem is resolved if there is no internal member aggregate to be initialized, i.e.

    std::pair<int, int> b[3] = {{1, 11}, {2, 22}, {3, 33}};
    

    will compile just fine as aggregate initialization.

  2. (The way you meant it.) You perform a brace-elided initialization, the innermost braces therefore are for aggregate-initialization of the individual pairs, while the braces for the internal array representations are elided. Note that even if there wasn't this ambiguity, as correctly pointed out in rustyx's answer, the rules of brace elision do not apply as std::pair is no aggregate type so the program would still be ill-formed.

The compiler will prefer option 1. By providing the extra braces, you perform the full-brace initialization and lift any syntactical ambiguity.


The C++14 brace elision rule applies only to subaggregate initialization.

So for example something like this works:

std::array<std::array<int, 3>, 3> a{1, 11, 2, 22, 3, 33};

Here an aggregate of aggregates can be list-initialized without extra braces.

But std::pair is not an aggregate (it has constructors), so the rule does not apply.

Which means that without the brace elision rule, std::array, itself being an aggregate with an array inside, needs an extra set of braces in order to be list-initialized. Remember that the class template array is implemented as:

template<typename T, std::size_t N> 
struct array {
  T elems[N];
};

To list-initialize it without the brace elision rule, you need an extra set of braces to get to the elems member.


Without the double braces, the statement is simply ambiguous. Consider the following code:

    std::array<std::pair<int, int>, 1> a = {{ {1, 2} }};
    std::array<int, 2> b = { {1, 2} };

Without double braces in the first definition, the compiler will treat { {1,2} } as a scalar initialization list for array<int, 2>. You need to declare an explicit nested braced-init-list in order for the compiler to recognize that the inner list is also aggregate-initialized (vs. scalar initialized), such that it can construct an array of std::pair.