D
David Sanders
Hi,
I have a class with an integer template parameter, taking values 1, 2
or 3, and a function 'calc' in that class which performs
calculations. Some calculations need only be performed if the
template parameter is 2 or 3; for efficiency, I do not wish to perform
the calculations if the template parameter is 1.
I currently do this as follows:
template<int d>
class myclass {
myclass() {
}
void calc() {
// calculation for d==1 goes here -- performed also if d==2 or
d==3
if (d==2) ...
if (d==3) ...
}
};
My question is, how can I replace these tests with a compile-time test
of the value of the template parameter to include only the relevant
code? I have tried #if, but this does not see the value of the
template parameter.
As far as I understand, one solution would be to put the d==2 and d==3
cases in a separate function and use template specialization? But
this seems to be overkill in this simple case.
Thanks and best wishes,
David.
I have a class with an integer template parameter, taking values 1, 2
or 3, and a function 'calc' in that class which performs
calculations. Some calculations need only be performed if the
template parameter is 2 or 3; for efficiency, I do not wish to perform
the calculations if the template parameter is 1.
I currently do this as follows:
template<int d>
class myclass {
myclass() {
}
void calc() {
// calculation for d==1 goes here -- performed also if d==2 or
d==3
if (d==2) ...
if (d==3) ...
}
};
My question is, how can I replace these tests with a compile-time test
of the value of the template parameter to include only the relevant
code? I have tried #if, but this does not see the value of the
template parameter.
As far as I understand, one solution would be to put the d==2 and d==3
cases in a separate function and use template specialization? But
this seems to be overkill in this simple case.
Thanks and best wishes,
David.