I am trying to initialize a 2D std::array trough initializer lists however the compiler tells me that there are too many initializers.
e.g.:
std::array<std::array<int, 2>, 2> shape = { {1, 1},
{1, 1} };
Compiler error: error: too many initializers for ‘std::array<std::array<int, 2ul>, 2ul>’
But clearly there aren't too many. Am I doing something wrong?
Initialization of std::array Like arrays, we initialize an std::array by simply assigning it values at the time of declaration. For example, we will initialize an integer type std::array named 'n' of length 5 as shown below; std::array<int, 5> n = {1, 2, 3, 4, 5};
The general syntax of declaring an array container is: array<object_type, size> array_name; The above declaration creates an array container 'array_name' with size 'size' and with objects of type 'object_type'.
An object of type std::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of type const T .
The initializer list is used to directly initialize data members of a class. An initializer list starts after the constructor name and its parameters.
Try to add one more pair {}
to ensure we're initializing the internal C array.
std::array<std::array<int, 2>, 2> shape = {{ {1, 1},
{1, 1} }};
Or just drop all the brackets.
std::array<std::array<int, 2>, 2> shape = { 1, 1,
1, 1 };
I would suggest (without even have trying it, so I could be wrong)
typedef std::array<int, 2> row;
std::array<row,2> shape = { row {1,1}, row {1,1} };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With