C++ 多维std::initializer_列表,其中将维度数指定为其类的模板参数
我有一个多维数组,它目前不接受初始化器列表初始化,但我想允许它。然而,似乎我不能基于模板参数为std::initializer\u列表指定任意数量的嵌套C++ 多维std::initializer_列表,其中将维度数指定为其类的模板参数,c++,templates,gcc,c++11,template-meta-programming,C++,Templates,Gcc,C++11,Template Meta Programming,我有一个多维数组,它目前不接受初始化器列表初始化,但我想允许它。然而,似乎我不能基于模板参数为std::initializer\u列表指定任意数量的嵌套 // e.g. 5D array: array<int, 5> arr(10, 5, 20, 34, 10); // the integers are the lengths of each dimension. // This is what I'm trying to achieve on top of the above:
// e.g. 5D array:
array<int, 5> arr(10, 5, 20, 34, 10); // the integers are the lengths of each dimension.
// This is what I'm trying to achieve on top of the above:
// creates the array and initializes it
array<int, 5> arr{ {{{{0, 1}}}}, {{{{1, 1}}}} };
// class signature:
// template <template T, unsigned dimensions> array { ... }
// --> where T is the array element type
//例如5D阵列:
阵列arr(10,5,20,34,10);//整数是每个维度的长度。
//这就是我试图在上述基础上实现的目标:
//创建数组并初始化它
数组arr{{{{{0,1}}},{{{{{1,1}}};
//班级签名:
//模板数组{…}
//-->其中T是数组元素类型
答案不一定非得使用
std::initializer\u list
我认为这应该有效:
template<typename BASE, int N>
struct nested {
typedef std::initializer_list<typename nested<BASE,N-1>::initializer_list> initializer_list;
};
template<typename BASE>
struct nested<BASE,0> {
typedef BASE initializer_list;
};
template<typename BASE, int N>
struct multi {
multi(typename nested<BASE,N>::initializer_list& init) {
};
};
模板
嵌套结构{
typedef std::初始值设定项列表初始值设定项列表;
};
模板
嵌套结构{
typedef基初始值设定项\u列表;
};
模板
结构多{
multi(typename嵌套::初始值设定项\u list和init){
};
};
不幸的是,我的两个版本的gcc都没有可用的初始值设定项列表支持,所以我无法正确地测试它。好吧,
array
应该接受initializer\u list
,而array
应该接受initializer\u list
…你真的是指5d数组,还是仅仅是一个列不均匀的2d数组(锯齿数组)?@JesseGood:True多维数组。@ZachSaw:所以,要访问元素,可以执行以下操作array[1][1][1][1]代码>?@JesseGood:arr(1,1,1,1)。