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
- C++ 中Traits技术 (3) —— 关于特化和偏特化
- C++—模板特化和偏特化
- C++——模板特化和偏特化
- C++——模板特化和偏特化
- C++——模板特化和偏特化
- C++——模板特化和偏特化
- C++ — 全特化和偏特化
- Traits技术 & 偏特化(partial specialization):
- traits(萃取)和 偏特化
- 特化和偏特化
- 特化和偏特化
- 特化和偏特化
- 特化和偏特化
- 模板特化和偏特化
- 模板特化和偏特化
- 模板特化和偏特化
- 模板特化和偏特化
- 模板特化和偏特化 .
- 多播(组播)
- 递归的全排列(耗时比较长)
- 码农小汪-设计模式-模板方法
- 图像恢复技术之Matlab中的维纳滤波代码复现
- 一个大四求职菜鸟的web前端之路(终)
- C++ 中Traits技术 (3) —— 关于特化和偏特化
- HashMap学习笔记
- Qt学习记录--03 Qt的信号槽的使用
- 原生 js 实现全屏滚动效果
- LeetCode——034
- lintcode之最长公共前缀
- JavaWeb企业实训 简易股市自选查询分析系统(三)前端页面加入highchart及CSS3列表
- 易生活(一)-web---商品上传、smartupload使用、虚拟路径
- Hadoop学习