We can alias a multidimensional array like this:
template<typename T, size_t size1, size_t size2>
using myArray = std::array<std::array<T, size2>, size1>;
But this only allows us a predefined number of dimensions. Is there a way to turn this into a variadic template, so that we could write any of those:
myArray<int, 2, 2, 2> arr3d;
myArray<int, 2, 2, 2, 2> arr4d;
I've tried a few things but wasn't completely satisfied with any of them.
This:
template<typename T, size_t size>
using myArray<T, size> = std::array<T, size>;
template<typename T, size_t size, size_t... more>
using myArray = std::array<myArray<T, more...>, size>;
doesn't even compile because template specializations are apparently not allowed with alias templates.
This is currently my best solution but deletes all constructors of std::array which I would like to keep:
template<typename T, size_t size, size_t... more>
struct myArray : public std::array<myArray<T, more...>, size> {};
template<typename T, size_t size>
struct myArray<T, size> : public std::array<T, size>{};
And with this solution I would always have to write ".internal" in front of every array access:
template<typename T, size_t size, size_t... more>
struct myArr {
std::array<myArr<T, more...>, size> internal;
};
template<typename T, size_t size>
struct myArr<T, size> {
std::array<T, size> internal;
};
So could anyone think of a solution like the second but where I could keep the constructors? I can't.