// maximum of two int values inline int const& max (int const& a, int const& b) { return a < b ? b : a; } // maximum of two values of any type template <typename T> inline T const& max (T const& a, T const& b) { return a < b ? b : a; } /* 对于非模板函数和同名的函数模板,如果其他条件都相同的话,那么在调用的时候,重载解析过程通常 会调用非模板函数,而不会从该模板产生出一个实例。 */ // maximum of three values of any type template <typename T> inline T const& max (T const& a, T const& b, T const& c) { return ::max (::max(a,b), c); } int main() { ::max(7, 42, 68); // calls the template for three arguments ::max(7.0, 42.0); // calls max<double> (by argument deduction) ::max('a', 'b'); // calls max<char> (by argument deduction) ::max(7, 42); // calls the nontemplate for two ints ::max<>(7, 42); // calls max<int> (by argument deduction) //显式地指定一个空的模板实参列表,这个语法告诉编译器:只有模板才能来匹配调用 ::max<double>(7, 42); // calls max<double> (no argument deduction) ::max('a', 42.7); // calls the nontemplate for two ints //模板不允许自动类型转化,但普通函数可以进行自动类型转化,所以此时将调用非模板函数 }