C++ 基于std::array的多维数组

C++ 基于std::array的多维数组,c++,templates,variadic-templates,c++17,stdarray,C++,Templates,Variadic Templates,C++17,Stdarray,我需要一个基于std::array的多维数组模板 template <typename T, size_t...> using MyArray = ? // -> here is something I don't know how to write... 模板 使用MyArray=?/->这是我不知道怎么写的东西。。。 用法如下: static_assert(std::is_same_v< MyArray<int, 2>, std::array<

我需要一个基于
std::array
的多维数组模板

template <typename T, size_t...>
using MyArray = ? // -> here is something I don't know how to write...
模板
使用MyArray=?/->这是我不知道怎么写的东西。。。
用法如下:

static_assert(std::is_same_v<
  MyArray<int, 2>, std::array<int, 2>
>);

static_assert(std::is_same_v<
  MyArray<int, 2, 3>, std::array<std::array<int, 3>, 2>
>);  // the dimension should be the opposite order

MyArray a;    // a stacked 2-D array of size 2x3
a[0][2] = 2;  // valid index
静态断言(std::is_same_v<
MyArray,std::array
>);
静态断言(std::is_same_v)<
MyArray,std::array
>);  // 尺寸应为相反的顺序
MyArray a;//尺寸为2x3的堆叠二维阵列
[0][2]=2;//有效索引

任何帮助都将不胜感激

我不知道如何只用
来制作;我所能想象的最好的需要助手结构的帮助

如下

template <typename, std::size_t ...>
struct MyArrayHelper;

template <typename T, std::size_t D0, std::size_t ... Ds>
struct MyArrayHelper<T, D0, Ds...>
 { using type = std::array<typename MyArrayHelper<T, Ds...>::type, D0>; };

template <typename T>
struct MyArrayHelper<T>
 { using type = T; };

template <typename T, std::size_t ... Ds>
using MyArray = typename MyArrayHelper<T, Ds...>::type;
模板
结构MyArrayHelper;
样板
结构MyArrayHelper
{using type=std::array;};
样板
结构MyArrayHelper
{使用type=T;};
样板
使用MyArray=typename MyArrayHelper::type;

您真的需要多维数组吗?通常可以使用m*n大小的数组,而不是n大小数组的m个实例。然后你可以用简单的访问函数和变异函数来模拟二维行为