C++的模板特化 和 STL中iterator_traits模板的偏特化
C++中有类模板和函数模板,它们的定义如下所示:
类模板:
template<class T1,class T2> class C { //... };
函数模板:
template<class T1,class T2> void func(T1 p1,T2 p2) { //... };
特化包括全特化和偏特化,全特化也叫简称特化,所以说特化的时候意思就是全特化。
特化就是对所有的模板参数指定一个特定的类型,偏特化就是对部分模板参数指定特定的类型。
类模板的特化:
template<class T1,class T2> //这里是类模板 class C { //... }; template<> //这里是对C的特化 class C<int,int> { //... };
从语法上看,全特化的时候,template后面尖括号里面的模板参数列表必须是空的,表示该特化版本没有模板参数,全部都被特化了。
类模板的偏特化:
template<class T1,class T2> //这里是类模板 class C { //... }; template <class T1> //这里是对C的偏特化 class C<T1,int> { //... };
从语法上看,偏特化的时候,template后面的尖括号里面的模板参数列表必须列出未特化的模板参数。同时在C后面要全部列出模板参数,同时指定特化的类型,比如指定int为T2的特化类型。
函数模板的特化:
template<class T1,class T2> //这里是函数模板 void func(T1 p1,T2 p2) { //... }; template <> //这里是对函数模板的特化 void func(int p1,int p2) { //... };
函数模板的偏特化:
函数模板不能进行偏特化!!!!!
//--------------------------------------------------------
STL中iterator_traits的偏特化
STL中的iterator_traits用来返回iterator中定义好的五种类型。其具体实现应该是:
template<class Iterator> struct iterator_traits { typedef typename Iterator::difference_type difference_type; typedef typename Iterator::value_type value_type; typedef typename Iterator::pointer pointer; typedef typename Iterator::reference reference; typedef typename Iterator::iterator_category iterator_category; };
因为指针也是一种iterator,如果将指针作为模板参数,上面的定义就会失效。因此上面的模板必须对指针进行特化。特化的代码如下所示:
template<class T> struct iterator_traits<T*> { typedef ptrdiff_t difference_type; typedef T value_type; typedef T* pointer; typedef T& reference; typedef random_access_iterator_tag iterator_category; };
而且还需要对 const T* 进行特化,否则 value_type 就成了 const T 类型,这通常不是我们想要的.
template<class T> struct iterator_traits<const T*> { typedef ptrdiff_t difference_type; typedef T value_type; typedef const T* pointer; typedef const T& reference; typedef random_access_iterator_tag iterator_category; };
内容主要来自: http://blog.csdn.net/timewalker08/article/details/7195698
C++的模板特化 和 STL中iterator_traits模板的偏特化的更多相关文章
- 模板引擎-vue中的模板如何被解析,指令如何处理
模板是什么 <div id='app'> <div> <input v-model="title"/> <button v-on:clic ...
- C++中模板的特化与偏特化
1.引言 C++中的模板分为类模板和函数模板,虽然它引进到C++标准中的时间不是很长,但是却得到了广泛的应用,这一点在STL中有着充分的体现.目前,STL在C++社区中得到了广泛的关注.应用和研究.理 ...
- [转]C++中模板的特化与偏特化
转载自:http://hi.baidu.com/klcdyx2008/blog/item/5adbf77b79f316f90bd1873c.html 1.引言C++中的模板分为类模板和函数模板,虽然它 ...
- STL中实现 iterator trail 的编程技巧
STL中实现 iterator trail 的编程技巧 <泛型编程和 STL>笔记及思考. 这篇文章主要记录在 STL 中迭代器设计过程中出现的编程技巧,围绕的 STL 主题为 (迭代器特 ...
- C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)
1. 主版本模板类 首先我们来看一段初学者都能看懂,应用了模板的程序: 1 #include <iostream> 2 using namespace std; 3 4 template ...
- 如何合理利用iMindMap中的模板创建思维导图
思维导图的制作并不是一项简单的工作,尤其是对许多工作或学习有特殊要求的朋友而言,当我们需要应对不同场景制作不同的思维导图时,总不能都靠自己从头制作,这样难度比较大也比较耗时.而iMindMap(win ...
- 3.2 STL中的函数对象类模板
*: STL中有一些函数对象类模板,如下所示: 1)例如要求两个double类型的x 和y 的积,可以: multiplies<double>()(x,y); 该表达式的值就是x*y的值. ...
- C++的标准模板库STL中实现的数据结构之顺序表vector的分析与使用
摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解.即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第一篇,主要针对线性表中的顺序表(动 ...
- C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用
摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解,即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第二篇.主要针对线性表中的链表 ST ...
随机推荐
- 【hiho一下第77周】递归-减而治之 (MS面试题:Koch Snowflake)
本题是一道微软面试题,看起来复杂,解出来会发现其实是一个很简单的递归问题,但是这道题的递归思路是很值得我们反复推敲的. 原题为hihocoder第77周的题目. 描述 Koch Snowflake i ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Reverse Words in a String(hard)☆
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 【python】lxml处理命名空间
有如下xml <A xmlns="http://This/is/a/namespace"> <B>dataB1</B> <B>dat ...
- Odoo创建数据库时出现的问题 DataError: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)
解决方案: 执行如下指令进入PostgreSQL控制台: sudo -u postgres psql postgres 然后在PostgreSQL控制下按顺序执行如下指令: update pg_dat ...
- io流对文件读写操作
public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedRead ...
- 通过xib加载UITableViewCell的新方式
我们以前通常会这样做 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPa ...
- September 2nd 2016 Week 36th Friday
How does the world look through your eyes? 你眼里的世界是什么样子的? How does the world look through your eyes? ...
- Sightseeing(poj 3463)
题意:给出n个点m条单向边,求最短路的道路条数和比最短路大1的道路条数的和. /* 用Dijkstra更新2*n次,来更新出所有点的最短路和次短路,顺便更新方案数. */ #include<cs ...
- Java返回距离当前时间段
/** * 计算该时间离当前时间的差距 * @param time 格式为:yyyy-MM-dd HH:mm:ss * @return */ public static String getShort ...