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

std::array constructor
std::array fill
std::array size
std::array copy
std::make_array
std::array default initialization
c++ array initialization
c initialize array to 0

If I initialize a std::array as follows, the compiler gives me a warning about missing braces

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

This fixes the problem:

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

This is the warning message:

missing braces around initializer for 'std::array<int, 4u>::value_type [4] {aka int [4]}' [-Wmissing-braces]

Is this just a bug in my version of gcc, or is it done intentionally? If so, why?

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

std::array - cppreference.com, C11 standard (ISO/IEC 9899:2011):. 6.7.9/12-38 Initialization (p: 140-144). C99 standard (ISO/  C++11: Correct std::array initialization? Ask Question Asked 6 years, 11 months ago. Active 6 months ago. Viewed 64k times 60. 7. If I initialize a std::array as

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

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

Array initialization - cppreference.com, Like arrays, we initialize an std::array by simply assigning it values at the time of If you are getting any compilation error, instruct your compiler to use C++11 It is used to automatically assign the correct type to the variable i by the compiler. std::array<> is introduced in c++11 and it’s a wrapper around old C style array, with added advantages. It’s is a kind of sequential container with constant size elements. std::array is internally defined as class template i.e. template < class T, size_t N > class array; template < class T, size_ t N > Here first template parameter T is the

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 initialization
std::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

C++ std::array: declare, initialize, passing std::array to function, Although both are built right into the C++ language, they both have downsides: Introduced in C++11, std::array provides fixed array functionality that won't std::​array<int, 5> myArray2 { 9, 7, 5, 3, 1 }; // uniform initialization  Before learning about std::array, let's first see the need for it. std::array is a container that wraps around fixed size arrays. It also doesn't loose the information of its length when decayed to a pointer. You know that when we pass an array (also known as C-style array) to a function, the address of the array gets passed to the function i.e

6.15, To initialize an array in C/C++ with the same value, the naive way is to we can use std::fill_n in C++, that assigns a value to the first n elements of the array. Introduced in C++11, std::array provides fixed array functionality that won’t decay when passed into a function. std::array is defined in the <array> header, inside the std namespace. Declaring a std::array variable is easy: #include <array> std::array<int, 3> myArray; // declare an integer array with length 3.

Initialize all elements of an array to same value in C/C++, You have to keep the rule for aggregate initialization in mind: std::array<int,10> arr: The 10 elements are not initialized. std::array<int,10>  std::array is a container that encapsulates fixed size arrays. This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member. Unlike a C-style array, it doesn't decay to T* automatically. As an aggregate type, it can be initialized with aggregate-initialization given at

std::array - Dynamic Memory, no Thanks , The C Standard also dictates how array initialization is handled when the size of the array provides a redundancy check, ensuring that the array's size is correct. See STR11-C. Do not specify the bound of a character array initialized with a  Nothing has substantially changed between C++03 and C++11+ for code that is valid in both languages. Note that for SDMs that are not inline, the in-class declaration is not a definition—regardless of whether an initializer is provided—and they must be defined if they are odr-used.

Comments
  • std::array is an aggregate. I think they might be making it work with one set in the future, however.
  • @chris What exactly, do you mean by that?
  • Well, you know how you can have struct S {int i; int j;}; and initialize it using S s = {5, 6};? That's aggregate initialization. std::array contains a built-in array, which can be initialized via an initializer list, which is what the inner set is. The outer set is for aggregate initialization.
  • @chris So in my case "{1, 2, 3, 4}" is just a std::initializer_list object which itself has to be placed within the actual initialization braces?
  • Well, I'm not overly sure of how built-in array initializer lists are handled after the introduction of that type, but that's the gist of it, yes. One's for the class, and the other is for the array inside the class.
  • All versions of the standard allow brace elision.
  • Huh, stupid GCC warnings >.> I wasn't aware that it was the case already.
  • I had the same problem (2016 by now), but I fixed it with this syntax: 'std::array<int,4> a[] = {1,2,3,4};' So I added square braces instead of nested curly braces. Maybe someone knows why this variant worked for me?
  • @Sam That has a different meaning. The syntax you posted is creating an array of std::arrays (a 2-dimensional array), rather than a single array (1-dimensional).
  • @cyberpunk_ only if your compiler implemented DR #1270 which lifts that restriction.
  • @Chubbi But why does it give me a warning for "std::array<int, 4> a = {1, 2, 3, 4}" then?
  • @cyberpunk_ It's just a bogus warning.
  • @Cubbi But annoying nonetheless. There has to be a way to get rid of it.
  • @cyberpunk_ You can trivially satisfy it with the extra braces. It's not the only annoying warning GCC has (ever seen suggest parentheses around ‘&&’ within ‘||’?)