从一些关于模板专业化的幻灯片:
#include <iostream>
using namespace std;
template<class X>
X& min(X& a, X& b)
{
return a > b ? b : a;
}
int& min(int& a, int & b)
{
// rewrite of the function in the case of int:
cout << "int explicit function\n";
return a > b ? b : a;
}
/*
new syntax – the more appropriate way:
template<>
int& min<int>(int& a, int& b)
{
cout << "int explicit function\n";
return a > b ? b : a;
}
*/
为什么第二种方式更“合适”?