函式樣板的明確特製化
您可以使用函式樣板,為該型別提供函式樣板的明確特製化 (覆寫),以定義特定類型的特殊行為。 例如:
template<> void MySwap(double a, double b);
這項宣告可讓您定義不同的函式,如雙變數。 喜歡非樣板函式,標準的型別轉換 (例如升級型別的變數浮點數 到 雙) 會套用。
範例
// explicit_specialization.cpp
template<class T> void f(T t)
{
};
// Explicit specialization of f with 'char' with the
// template argument explicitly specified:
//
template<> void f<char>(char c)
{
}
// Explicit specialization of f with 'double' with the
// template argument deduced:
//
template<> void f(double d)
{
}
int main()
{
}