I have the following template:
#include <iostream>
template <class T,T defaultVal, int dim=255>
class Vec
{
T _vec[dim];
int _dim;
public:
Vec () : _dim(dim)
{
for (int i=0;i<_dim;++i)
{
_vec[i] = defaultVal;
}
}
~Vec () {};
// other operators and stuff
};
int main ()
{
int defValue = 0;
Vec < int,defValue > vecWithDefVal;
}
But the program will not compile because a template value must be known during compilation time, meaning const or const-literal.
I really dont understad this error, can anyone explain it to me?
Template class is created at compile time, so the value has to be known at compile time. If it is not
constit is not known until run-time, so the compiler can’t create the template class.