共用方式為


函式樣板的明確特製化

透過函式樣板,您可以提供該類型的明確特製化 (覆寫) 函式樣板,來定義該特定類型的特殊行為。 例如:

template<> void MySwap(double a, double b);

此宣告可讓您定義 double 變數的不同函式。 如同非樣板函式,會套用標準類型轉換(例如將 類型的 float 變數升階為 double )。

範例

// 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()
{
}

另請參閱

函式樣板