Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are "double braces" needed in declaration of multi-dimensional array using stacked std::array?

#include <array>
using std::array;

constexpr auto d1=2;
constexpr auto d2=3;
constexpr auto d3=4;

// stacked std::array
using arr_t   = array<int,d1>;
using arr2d_t = array<arr_t,d2>;
using arr3d_t = array<arr2d_t,d3>;
constexpr arr3d_t arr1 = {{
    {{  {1,2},  {3,4},  {5,6}  }},
    {{  {1,2},  {3,4},  {5,6}  }},
    {{  {1,2},  {3,4},  {5,6}  }},
    {{  {1,2},  {3,4},  {5,6}  }}
}};

// built-in array
using carr3d_t = int[d3][d2][d1];
constexpr carr3d_t arr2 = {
    { {1,2}, {3,4}, {5,6} },
    { {1,2}, {3,4}, {5,6} },
    { {1,2}, {3,4}, {5,6} },
    { {1,2}, {3,4}, {5,6} }
};

Although, one can get away all the braces business with only pair of single braces like these:

// getaway with one-dimensional declaration
constexpr arr3d_t arr3 = {
    1,2,  3,4,  5,6,
    1,2,  3,4,  5,6,
    1,2,  3,4,  5,6,
    1,2,  3,4,  5,6
};
constexpr carr3d_t arr4 = {
    1,2,  3,4,  5,6,
    1,2,  3,4,  5,6,
    1,2,  3,4,  5,6,
    1,2,  3,4,  5,6
};

I'd like to know why {{ are required except on the lowest dimension when using stacked std::array?

godbolt.org/g/b6qfn4

like image 240
sandthorn Avatar asked Dec 15 '17 19:12

sandthorn


2 Answers

The outer braces are aggregate initializer syntax and the inner are array initializer syntax.

C++14 allows brace elision. Make sure you are compiling with C++14 or better.

like image 89
Dúthomhas Avatar answered Nov 12 '22 00:11

Dúthomhas


Your types arr_t, arr2d_t, arr3d_t and carr3d_t are aggregate types. They are aggregate types because the std::array itself is an aggregate type. Their objects are initialized using aggregate initialization and when compiling with the C++11 support you need the (extra) {} braces surrounding the nested initializer(s). Starting with the C++14 standard:

the braces around the nested initializer lists may be elided (omitted)

like image 3
Ron Avatar answered Nov 11 '22 23:11

Ron