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

本文介绍了C++中Traits技术的特化和偏特化概念,通过示例详细讲解了全特化和偏特化的不同,并特别讨论了指针类型特化时const T*与T*的区别,强调了类型修饰符在编译时的信息获取重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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 * &及此类的组合修饰符。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值