Currently, I have the following template without templates:
class Vector{
public:
double data[3];
};
static Vector *myVariable;
void func() {
myVariable->data[0] = 0.;
}
int main() {
myVariable = new Vector();
func();
}
Then I want to change the dimension:
template<int DIM> class Vector{
public:
double data[DIM];
};
static Vector<3>* myVariable;
void func() {
myVariable->data[0] = 0.;
}
int main() {
myVariable = new Vector<3>();
func();
}
But I finally also want to change my variable, with the size:
template<int DIM> class Vector{
public:
double data[DIM];
};
template<int DIM> static Vector<DIM> *myVariable;
void func() {
myVariable->data[0] = 0.;
}
int main() {
int dim = 3;
if (dim==3)
myVariable = new Vector<3>();
else
myVariable = new Vector<4>();
func();
}
However, this latest version of the code causes an error: this static variable cannot be templated ("C2998: Vector * myVariable cannot be a template definition").
How can I fix this error without a complete reorganization (for example, to inherit the Vector template class from a non-templated class, which will require more expensive calls to virtual methods or manually create several myVariables of different sizes)? Maybe I'm just tired and don’t see the obvious answer: s
: , , , . , , , .
!