Given a template
template <int n>
void f(){...};
I know I can specialize it for specific values of n by doing:
template <>
void f<2>(){...};
But, is there a method which allows me to specialize it for all positive n?
I thought of doing the following
template <int n>
void f<n>(){
int dummy[n]; //invalid for n < 0
...
};
So for n<0 this code is invalid and the compiler would resort to the previous definition. Unfortunately, all I get is a redefinition of 'void f<n>()' error.
Note: I’m guessing this is probably not supported by the standard. I’m asking if there isn’t some method (maybe some template metaprogramming) to achieve this effect.
One option would be to use another level of indirection. Define an auxiliary template that takes in two arguments – the number
nand aboolrepresenting whether or notnis negative, then specialize that template for whennis negative. Then, have yourffunction instantiate the template with the right arguments.For example:
Another option is to use SFINAE overloading and the
std::enable_iftemplate class from C++11 (or Boost’s equivalent);Each of these functions will only be available for overload resolution if
nhas the proper sign, so the correct version will always be called.Hope this helps!