C++ 中Traits技术 (3) —— 关于特化和偏特化

来源:互联网 发布:三国杀 知乎 编辑:程序博客网 时间:2024/06/05 05:55

以下面这个例子为例进行说明:

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*> {typedef random_access_iterator_tag iterator_category;typedef _Tp value_type;typedef ptrdiff_t difference_type;typedef _Tp* pointer;typedef _Tp& reference;};

除了上面特化为T*,也可以是const T*,T&,const T&等。

如特化为const T*:

template<class T>class Test<T*>{public:    static bool TestFun(const T* t);};

  ③ 转化为另外一个 类模板

template<class T>class Test<vector<T> >{public:    static bool TestFun(const vector<T>& t);};

注:这种特化并不是完全的特化,比如T仍然可以是int double string等类型。


特化分为两种:全特化和偏特化。

全特化:模板中的模板参数全部指定为确定的类型。【template<>模板参数列表为空 因为所以参数都指定为特定的类型了

偏特化:模板中的模板参数未全部确定,需要在编译时确定。【函数模板不能偏特化!!

对于指针类型的偏特化,还有一点需要注意,T*和const T*的偏特化是不一样的。

如:iterator_traits<const T*>::value_type 就是const T,一般情况下,这是我们不希望的结果。

因此,需要对const T*和T*分别偏特化。


需要注意的是:

类型前加上的诸如 const * &及此类的组合修饰符。

这些并没有产生新的类型,仅仅只是类型修饰符而已,这些信息在编译时都是可以得到的。

0 0