// 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
//模闆不允許自動類型轉化,但普通函數可以進行自動類型轉化,是以此時将調用非模闆函數
}