我有利用初始化列表两班。一个是保存值列表的矢量风格类。
Vec.h:
template< typename T, int nDimensions = 2 >
class Vec{
private:
std::array< T, nDimensions > elements_;
public:
template <typename... U>
Vec(U... ts) : elements_{ ts... } {}
}
然后,使用时:
typedef Vec< int, 2 > Vec2i;
Vec2i twoi = { 1,2 };
Vec2i twoi2 = twoi; //I have copy constructor and other methods finished.
这个类的工作都很好,很正常,但是当我尝试使用这个类与我的风格矩阵类,我找不出其构造正确的语法。
template< typename T, int X,int Y>
class Mat {
private:
std::array< Vec<T, X>, Y > elements_;
}
我还想用它像这样:
typedef Mat<int, 3,3> Mat3i;
Mat3i threemat = { {1,2,3},
{4,5,6},
{7,8,9}};
现在,四尝试使用初始化列表与一些成功的构造,但我不能找出语法传递下去子列表。
Mat(std::initializer_list<Vec<T, X>> values) {
for (auto& t : values) {
//What goes here?
}
}
iv,也尝试了列表迭代和手动分配他们,但那是一个没有去。
它也是值得注意的,其重要的是,这些课程有没有列出的内存连续区块,并没有其他的变量。否则ID来使用其它类型的,而不是的std ::阵列。 (铸造和工会的目的。)
我辩论,如果我需要重新诠释投每个值作为A VEC然后在值复制。