模板的特化是指对特定的类型,进行特定的实现。
// general version
template<class T>
class Compare
{
public:
static bool IsEqual(const T& lh, const T& rh)
{
return lh == rh;
}
};
// specialize for float
template<>
class Compare<float>
{
public:
static bool IsEqual(const float& lh, const float& rh)
{
return abs(lh - rh) < 10e-3;
}
};
偏特化是指对特定的类型,进行特定的实现,但其中有一些是非特定的。
template<class T1, class T2>
class A
{
}
template<class T1>
class A<T1, int>
{
}
函数模板不允许被特化和偏特化,但函数模板允许重载。