以下面这个例子为例进行说明:
template<class T>
class Test
{
public:
static bool TestFun(const T& t);
};
特化,其实就是为模板参数指定特定类型,分为三种情况:
① 特化为具体类型
T可以是int string double等类型。如特化为int:
template<>
class Test<int>
{
public:
static bool TestFun(const int& t);
};
② 特化为引用和指针类型
这个出现在STL源码中的iterator_traits。
template <class _Iterator>
struct iterator_traits {
typedef typename _Iterator::iterator_category iterator_category;
typedef typename _Iterator::value_type value_type;
typedef typename _Iterator::difference_type difference_type;
typedef typename _Iterator::pointer pointer;
typedef typename _Iterator::reference reference;
};
// specialize for _Tp*
template <class _Tp>
struct iterator_traits<_Tp*> {
typede