在现代c++中优雅地定义多维数组

时间:2022-11-20 21:34:13

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.

使用T[][][]语法定义多维数组很容易。然而,这创建了一个原始数组类型,它不能很好地适应现代c++。这就是为什么我们有std::数组,因为c++ 11。但是使用std::array定义多维数组的语法非常混乱。例如,要定义一个三维int数组,必须编写std::array <:array std::array> , 5>, 5>。语法根本没有伸缩性。我要求解决这个问题。也许,这个问题不能通过使用c++提供的现有工具来解决。在这种情况下,我很乐意使用一个自定义工具来简化语法。 <:array>

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.

想知道实现是否可以进一步简化。

1 个解决方案

#1


8  

Refer to Multi-dimensional arrays in C++11

参考c++ 11中的多维数组

template <class T, std::size_t I, std::size_t... J>
struct MultiDimArray 
{
  using Nested = typename MultiDimArray<T, J...>::type;
  // typedef typename MultiDimArray<T, J...>::type Nested;
  using type = std::array<Nested, I>;
  // typedef std::array<Nested, I> type;
};

template <class T, std::size_t I>
struct MultiDimArray<T, I> 
{
  using type = std::array<T, I>;
  // typedef std::array<T, I> type;
};

MultiDimArray<float, 3, 4, 5, 6, 7>::type floats;

MultiDimArray is a pair of meta-functions to compute nested type for multi-dimensional std::array. The most general MultiDimArray is a variadic template of unsigned integers to pass an arbitrary number of dimensions. The terminating MultiDimArray specialization defines the simplest case of single dimensional std::array.

MultiDimArray是一对用于计算多维std::array的嵌套类型的元函数。最普遍的多二marray是一个无符号整数的可变单元模板,用于传递任意数量的维数。最终的多二聚体射线专门化定义了单维std::array的最简单情况。

#1


8  

Refer to Multi-dimensional arrays in C++11

参考c++ 11中的多维数组

template <class T, std::size_t I, std::size_t... J>
struct MultiDimArray 
{
  using Nested = typename MultiDimArray<T, J...>::type;
  // typedef typename MultiDimArray<T, J...>::type Nested;
  using type = std::array<Nested, I>;
  // typedef std::array<Nested, I> type;
};

template <class T, std::size_t I>
struct MultiDimArray<T, I> 
{
  using type = std::array<T, I>;
  // typedef std::array<T, I> type;
};

MultiDimArray<float, 3, 4, 5, 6, 7>::type floats;

MultiDimArray is a pair of meta-functions to compute nested type for multi-dimensional std::array. The most general MultiDimArray is a variadic template of unsigned integers to pass an arbitrary number of dimensions. The terminating MultiDimArray specialization defines the simplest case of single dimensional std::array.

MultiDimArray是一对用于计算多维std::array的嵌套类型的元函数。最普遍的多二marray是一个无符号整数的可变单元模板,用于传递任意数量的维数。最终的多二聚体射线专门化定义了单维std::array的最简单情况。