我不明白为什么模板参数只能用const变量初始化。 作为,为什么不将下面的代码工作:C++,模板的非参数类型:只有const变量?
#include <iostream>
template <class T,int dim>
class Vec
{
T _vec[dim];
int _dim;
public:
Vec() : _dim(dim) {};
~Vec() {};
// other operators and stuff
};
int main() {
int dim = 3;
Vec < int, dim> vecInt3;
}
如果我添加一个const在主,一切的朦胧的定义是罚款。这是为什么?
这样可以避免这个问题(在模板中不需要'dim'),但不能解释原因。 –
MSalters