函数模板的显式专用化

函数模板,可以通过提供显式专用化定义特定类型的特定行为 (重写) 函数模板为该类型。 例如:

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

此声明可以定义 二进制文件 变量的不同功能。 与非模板函数,标准类型转换 (例如提升类型 float 的变量。 二进制文件) 适用。

示例

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

请参见

参考

函数模板