#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} }
};
尽管如此,我们可以只使用一对这样的单引号来避免所有括号的麻烦:
// 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
};
我想知道为什么在使用堆叠的std :: array时,除了最低维度之外都需要
{{
?
godbolt.org/g/b6qfn4