Share via


関数テンプレートの明示的特殊化

関数テンプレートを使用すると、特定の型のために関数テンプレートの明示的な特殊化 (オーバーライド) を提供することによって、その型の特別な動作を定義できます。 次に例を示します。

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

関連項目

関数テンプレート