'C++: array<> too many initializers

The following code is returning the compilation error below. I'm stuck understanding how there are too many initializers. This code works using vector<X>. Does anyone know why the error is being reported and how to resolve? Thanks

#include <iostream>
#include <array>
using namespace std;

struct X {
    int x, y;
};

int main(int argc, char *argv[])
{
    array<X,2> a0 = {{0,1}, {2,3}};

    for (auto& p : a0) {
        cout << p.x << endl;
        cout << p.y << endl;
    }

    return 0;
}

Compilation:

g++ -pedantic -Wall test116.cc && ./a.out
test116.cc: In function ‘int main(int, char**)’:
test116.cc:11:34: error: too many initializers for ‘std::array<X, 2>’
     array<X,2> a0 = {{0,1}, {2,3}};


Solution 1:[1]

Try

array<X,2> a0 = {{{0,1}, {2,3}}};

Note the extra set of braces.

It seems a bit odd but it's this way because the only member of array is the actual array:

template <class T, size_t N>
class array {
    T val[N];
    // ...
};

The constructors are all implicitly defined so that array ends up being a trivially constructable type.

Solution 2:[2]

You may use one of the following initializations because std::array is an aggregate that contains another aggregate as its data member.

array<X,2> a0 = { { { 0, 1 }, { 2, 3 } } };
array<X,2> a0 = { 0, 1, 2, 3 };
array<X,2> a0 = { { 0, 1, 2, 3 } };
array<X,2> a0 = { { 0, 1, { 2, 3 } } };
array<X,2> a0 = { { { 0, 1 }, 2, 3 } };

Solution 3:[3]

I found that my problem is related to the version of g++,9.4.0 is ok and 5.4.0 not.version of g++.By the way, the version of g++ is associated with the system version generally.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arthur Tacca
Solution 2 Vlad from Moscow
Solution 3 Crawl.W