网络连接有一个矩阵类的一个模板:初始化矩阵类括号,同时类型安全
template<typename T, int rows, int cols>
struct Matrix{
public:
Matrix() {
if (rows == cols)
LoadIdentity();
}
T data[rows][cols];
void LoadIdentity(){ }
/* more methods ... */
}
template<int rows, int cols>
using matrixf = Matrix<float, rows, cols>;
template<int rows, int cols>
using matrixd = Matrix<double, rows, cols>;
,我希望能够初始化这个类,如:
void main(){
matrixf<2, 3> m2x3 = { { 1, 2, 3 }, {4, 5, 6} };
}
如果我试试这个,编译器说:
E0289没有构造函数“vian :: Matrix [with T = float,rows = 2,cols = 3]”的实例匹配参数列表
如果我删除我的默认构造函数,我得到我想要的行为,但我失去了任何构造函数的可能性。
Matrix() { ... } // If I remove this, I have the behaviour I want
一个solution I found是创建一个构造函数的的std :: initializer_list,但如果我这样做,编译器不会检查initialier名单有观点的权利量为N×M大小的矩阵。
编辑:添加LoadIdentity方法,以便编译。
'void main' - >这是C++吗? –
我们这个代码的用例是什么? –
'Matrix(std :: array,rows>)''? –
Jarod42