In this example, how is it possible to call the second function?
template<class T>
T square(T a) {
std::cout << "using generic version to square " << a << std::endl;
return a*a;
}
/// "int" is so special -- provide a specialized function template
template<>
int square(int a) {
std::cout << "using specialized generic version to square " << a << std::endl;
return a*a;
}
/// and there's one more: a non-template square function for int
int square(int a) {
std::cout << "using explicit version to square " << a << std::endl;
return a*a;
}
Thanks in advance!
Call the specialization by explicitly specifying the template argument:
square<int>(2);
Live Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With