Defining multi-dimensional array using the T[][][]
syntax is easy. However, this creates a raw array type which doesn't fit nicely into modern C++. That's why we have std::array
since C++11. But the syntax to define a multi-dimensional array using std::array
is quite messy. For example, to define a three-dimensional int
array, you would have to write std::array<std::array<std::array<int, 5>, 5>, 5>
. The syntax doesn't scale at all. I'm asking for a fix for this issue. Maybe, this issue cannot be fixed using existing utility provided by C++. In that case, I'm happy with a custom tool developed to ease the syntax.
Found a solution myself:
template <typename T, std::size_t n, std::size_t... ns>
struct multi_array {
using type = std::array<typename multi_array<T, ns...>::type, n>;
};
template <typename T, std::size_t n>
struct multi_array<T, n> {
using type = std::array<T, n>;
};
template <typename T, std::size_t... ns>
using multi_array_t = typename multi_array<T, ns...>::type;
Wondering whether the implementation can be further simplified.
typedef
's not acceptable:typedef std::array< std::array< std::array<int, 5 > > > threeD;
..threeD arr; arr[0][0][0] ...
? This example would have to be modified to allow for different types, obviously, but curious if that might help? – Semaphorec++11
. – Yseultastd::array
isn't guaranteed to be the same as that of a C=style array of arrays (at least in C++11, I'm not sure if that got fixed in C++14.) – Pentlanditestd::array
just a simple wrapper around raw array? – Azalea