STL 全特化/偏特化
template<class T>
class Compare
{
public:
static bool isEqual(const T& lh,const T& rh)
{
return lh==rh;
}
};
这是一个用于比较的类模板,里面可以有多种用于比较的函数, 以IsEqual为例。
一、特化为绝对类型
也就是说直接为某个特定类型做特化,这是我们最常见的一种特化方式, 如特化为float, double等
template<>
class Compare<float>
{
public:
static bool isEqual(const float &lh,const float& rh)
{
return abs(lh-rh)<10e-; //0.001
}
};
画红线的地方不可少。
int main()
{
int a=,b=;
cout<<Compare<int>::isEqual(a,b)<<endl;
float x=3.141,y=3.1415; //如果不要特化就不相等,用特化就相等
cout<<Compare<float>::isEqual(x,y)<<endl;
}
二、特化为引用,指针类型
这种特化我最初是在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*, 我们也可以将T特化为 const T*, T&, const T&等,以下还是以T*为例:
#include<iostream>
using namespace std; template<class T>
class Compare
{
public:
static bool isEqual(const T& lh,const T& rh)
{
return lh==rh;
}
}; template< class T>
class Compare<T*>
{
public:
static bool isEqual(const T* lh,const T* rh)
{
cout<<"这是调用了Compare<T*>类型"<<endl;
return Compare<T>::isEqual(*lh,*rh);
}
}; int main()
{
int a=,b=;
cout<<Compare<int>::isEqual(a,b)<<endl; cout<<Compare<int*>::isEqual(&a,&b)<<endl;//调用指针类型
}
输出:
1
这是调用了Compare<T*>类型
1
请按任意键继续. . .
后面的由于传入的是Compare<int*>类型,传入的指针类型,调用特化的指针类型函数。
这种特化其实是就不是一种绝对的特化, 它只是对类型做了某些限定,但仍然保留了其一定的模板性,这种特化给我们提供了极大的方便, 如这里, 我们就不需要对int*, float*, double*等等类型分别做特化了。
三、特化为另外一个类模板
这其实是第二种方式的扩展,其实也是对类型做了某种限定,而不是绝对化为某个具体类型,如下:
#include<iostream>
#include<vector>
using namespace std; template<class T>
class Compare
{
public:
static bool isEqual(const T& lh,const T& rh)
{
return lh==rh;
}
}; //specialize for float template<>
class Compare<float>
{
public:
static bool isEqual(const float &lh,const float& rh)
{
return abs(lh-rh)<10e-; //0.001
}
};
template< class T>
class Compare<vector<T> >
{
public:
static bool isEqual(const vector<T> &lh,const vector<T>& rh)
{
cout<<"调用了compare<vector<T> >"<<endl;
if(lh.size()!=rh.size()) return false;
else
{
for(int i=;i<lh.size();i++)
{
if(lh[i]!=rh[i])
return false;
}
}
return true;
}
};
int main()
{
int a=,b=;
cout<<Compare<int>::isEqual(a,b)<<endl; vector<int> x(,);
vector<int> y(,);
cout<<Compare<vector<int> >::isEqual(x,y);
}
这就把IsEqual的参数限定为一种vector类型, 但具体是vector<int>还是vector<float>, 我们可以不关心, 因为对于这两种类型,我们的处理方式是一样的,我们可以把这种方式称为“半特化”。
当然, 我们可以将其“半特化”为任何我们自定义的模板类类型:
#include<iostream>
#include<vector>
using namespace std; template<class T>
class Compare
{
public:
static bool isEqual(const T& lh,const T& rh)
{
return lh==rh;
}
}; //specialize for float template<>
class Compare<float>
{
public:
static bool isEqual(const float &lh,const float& rh)
{
return abs(lh-rh)<10e-; //0.001
}
}; //specialize for any template class Type
template<class T1>
struct SpecializedType
{
T1 x1;
T1 x2;
};
template<class T>
class Compare<SpecializedType<T> >
{
public:
static bool isEqual(const SpecializedType<T> & lh,const SpecializedType<T> & rh)
{
cout<<"使用了Compare<SpecailizedType<T> >"<<endl;
return Compare<T>::isEqual(lh.x1+lh.x2,rh.x1+rh.x2);
}
};
int main()
{ SpecializedType<int> s1={,};
SpecializedType<int> s2={,};
cout<<Compare<SpecializedType<int> >::isEqual(s1,s2)<<endl;
}
使用了Compare<SpecailizedType<T> >
1
请按任意键继续. . .
注意在
class Compare<SpecializedType<T> >
我们Compare传入的类型是SpecializedType<int> >,所以比较的类型是
SpecializedType<T> ,而不是T。
当然,我们还可以:
// custom template class
SpecializedType<float> s1 = {10.1f,10.2f};
SpecializedType<float> s2 = {10.3f,10.0f};
bool r6 = Compare<SpecializedType<float> >::IsEqual(s1, s2);
使用特化的类型float。
模板有两种特化,全特化和偏特化(局部特化)
模板函数只能全特化,没有偏特化(以后可能有)。
模板类是可以全特化和偏特化的。
全特化,就是模板中模板参数全被指定为确定的类型。
全特化也就是定义了一个全新的类型,全特化的类中的函数可以与模板类不一样。
偏特化,就是模板中的模板参数没有被全部确定,需要编译器在编译时进行确定。
在类型上加上const、&、*( cosnt int、int&、int*、等等)并没有产生新的类型。只是类型被修饰了。模板在编译时,可以得到这些修饰信息。
模板的特化是非常有用的。它像一个在编译期的条件判断。当编译器在编译时找到了符合的特化实现,就会使用这个特化实现。这就叫编译器多态(或者叫静态多态)。这种东西对编写基础库是很有用的。这也就是为何c++的基础库大量使用了模板技术,而且大量使用了特化,特别是偏特化。
在泛型中,利用特化类得到类新的特性,以便找到最适合这种特性的实现。而这一切都是在编译时完成。
转自:http://www.360doc.com/content/12/1210/12/9200790_253182813.shtml#
、
STL 全特化/偏特化的更多相关文章
- c++模板特化偏特化
模板为什么要特化,因为编译器认为,对于特定的类型,如果你对某一功能有更好地实现,那么就该听你的. 模板分为类模板与函数模板,特化分为全特化与偏特化.全特化就是限定死模板实现的具体类型,偏特化就是模板如 ...
- STL全特化与偏特化
在泛型编程中,常常会使用一些非完全泛型的类模板,这就是特化. 如何理解全特化呢?如上图所示,第一个template class是空间配置器的类模板,第二个就是一个全特化的template class. ...
- C++的模板特化 和 STL中iterator_traits模板的偏特化
C++中有类模板和函数模板,它们的定义如下所示: 类模板: template<class T1,class T2> class C { //... }; 函数模板: template< ...
- STL迭代器之一:偏特化
在stl的算法中运用容器的迭代器时,很可能经常会用到迭代器相应型别(例如迭代器所指物的型别),假设算法中有必要声明一个变量,以"迭代器所指对象的型别"为类型,如何是好,例如我们写一 ...
- C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)
1. 主版本模板类 首先我们来看一段初学者都能看懂,应用了模板的程序: 1 #include <iostream> 2 using namespace std; 3 4 template ...
- [C++ 2011 STL (VS2012 Update4) 源代码阅读系列(2)]熟悉一些宏定义和模版偏特化或叫模版专门化
[C++ 2011 STL (VS2012 Update4) 源代码阅读系列(2)]熟悉一些宏定义和模版偏特化或叫模版专门化 // point_test.cpp : 知识点练习和测试,用于单步调试,跟 ...
- C++ 模板的全特化与偏特化
模板为什么要特化,因为编译器认为,对于特定的类型,如果你能对某一功能更好的实现,那么就该听你的. 模板分为类模板与函数模板,特化分为全特化与偏特化.全特化就是限定死模板实现的具体类型,偏特化就是如果这 ...
- C++模板的偏特化与全特化
模板的声明 类模板和函数模板的声明方式是一样的,在类定义/模板定义之前声明模板参数列表.例如: // 类模板 template <typename T1, typename T2> cla ...
- C++模板函数只能全特化不能偏特化
C++模板函数只能全特化不能偏特化
随机推荐
- MFC通过ODBC连接mysql(使用VS2012编写MFC)
原创文章,转载请注明原文:MFC通过ODBC连接mysql(使用VS2012编写MFC) By Lucio.Yang 1.ODBC连接mysql 首先ODBC是什么呢? 开放数据库互连(Open Da ...
- 使用bootstrap做一个响应式的页面
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 《离散数学之把妹要诀》的js实现
网上看到一篇有意思的文章<离散数学之把妹要诀> 就用JS写了上面所讲的配对方式: 首先设定变量 // 男生理想列表 var menPreference = { A: [1, 2, 3, 4 ...
- codeforces 645 D. Robot Rapping Results Report 二分+拓扑排序
题目链接 我们可以发现, 这是一个很明显的二分+拓扑排序.... 如何判断根据当前的点, 是否能构造出来一个唯一的拓扑序列呢. 如果有的点没有出现, 那么一定不满足. 如果在加进队列的时候, 同时加了 ...
- tiny4412学习笔记-将uboot、zImage、文件系统烧到emmc中
1.首先还是要将u-boot写入SD卡中从SD卡启动. 使用读卡器将SD插入电脑中,使用umount卸载u盘, fdisk -l显示其挂载点为 /dev/sdb1 切换到/home/bunfly/im ...
- LINUX常用命令--重定向、管道篇(四)
一.Linux重定向 重定向能够实现Linux命令的输入输出与文件之间重定向,以及实现将多个命令组合起来实现更加强大的命令.这部分涉及到的比较多的命令主要有: 涉及到的比较多的命令主要有: cat:连 ...
- 《转》JAVA中PriorityQueue优先级队列使用方法
该文章转自:http://blog.csdn.net/hiphopmattshi/article/details/7334487 优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最 ...
- InputStream、OutputStream、String的相互转换(转)
//1.字符串转inputStream String string; //...... InputStream is = new ByteArrayInputStream(string.getByte ...
- VC中实现带有背景位图的树型控件
当前许多应用程序都在使用树型控件时为其添加了背景位图,增强的控件的魅力,然而对于Visual C++编程爱好者来说,使用Visual C++MFC提供的树型控件(CTreeCtrl)本身就是一个难点, ...
- UI_拖动View
方法一 在touchesMoved中 // 获取到触摸的手指 UITouch *touch = [touches anyObject]; // 获取集合中对象 // 获取開始时的触摸点 CGPoint ...