C++11: Correct std::array initialization? C++11: Correct std::array initialization? arrays arrays

C++11: Correct std::array initialization?


This is the bare implementation of std::array:

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

It's an aggregate struct whose only data member is a traditional array, such that the inner {} is used to initialize the inner array.

Brace elision is allowed in certain cases with aggregate initialization (but usually not recommended) and so only one brace can be used in this case. See here: C++ vector of arrays


According to cppreference. Double braces are required only if = is omitted.

// construction uses aggregate initializationstd::array<int, 3> a1{ {1,2,3} };    // double-braces requiredstd::array<int, 3> a2 = {1, 2, 3}; // except after =std::array<std::string, 2> a3 = { {std::string("a"), "b"} };


Double-braces required in C++11 prior to the CWG 1270 (not needed in C++11 after the revision and in C++14 and beyond):

// construction uses aggregate initializationstd::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 prior to the CWG 1270 revision                                    // (not needed in C++11 after the revision and in C++14 and beyond)std::array<int, 3> a2 = {1, 2, 3};  // never required after =

std::array reference