The key part is to make sure that the {} initialization works like std::array , and keeps itself as similar as reasonable. Compatibility with std::array also important, but what is more compatible than std::array ? So my solution generates a multidimensional array from std::array s:
template<class T, size_t... sizes> struct multi_array_helper { using type=T; }; template<class T, size_t... sizes> using multi_array = typename multi_array_helper<T, sizes...>::type; template<class T, size_t s0, size_t... sizes> struct multi_array_helper<T, s0, sizes...> { using type=std::array<multi_array<T, sizes...>, s0>; };
living example
Syntax Example:
multi_array<int, 2,2> arr = {{ {{0,1}}, {{2,3}} }}; static_assert( std::is_same< multi_array<int,2,2>, std::array<std::array<int,2>,2> >{}, "see, just arrays under the hood" );
A small optimization may include folding the entire hierarchy of the array if something has dimension 0, but I'm not sure.
A multi_array<int> is an int as a third-party (zero-dimensional array of int s), since it makes sense and because it makes the code simpler.
Here is the old version .
source share