像这样的东西也许
namespace mine {
template<typename T, size_t first, size_t... rest>
struct multi_array__ {
enum { dims = sizeof...(rest) };
static_assert(first,"dimension can not be zero!");
typedef std::array< typename multi_array__<T, rest...>::type, first > type;
};
template<typename T, size_t first>
struct multi_array__<T,first> {
enum { dims = 1 };
static_assert(first,"dimension can not be zero!");
typedef std::array<T,first> type;
};
template <typename T, std::size_t... ds>
using multi_array = typename multi_array__<T, ds ...>::type;
};
你可以像这样使用它
mine::multi_array <int,2,3,4> arr1 = {};
// multi_array<int,2,3,0,4> arr3; // This fails
mine::multi_array <int,3,4> arr2 = {};
像这样的作业也可以
arr2[0] = arr1[0][0];
Here
是一个简单的测试程序。