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++模板函数只能全特化不能偏特化
随机推荐
- 进程间通信机制IPC
进程通信是指进程之间的信息交换.PV操作是低级通信方式,例如信号量,主要是进程间以及同一进程内不同线程之间的同步手段.髙级通信方式是指以较高的效率传输大量数据的通信方式.高级通信方法主要有以下三个类. ...
- Python进阶之闭包
闭包 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB","S ...
- vb6.0 时间日期
使用year(now)可以得到4位数的年 你还可以用Format来得到, 还有FormatDateTime 下面两种都是一样的结果: FormatDateTime(now,vbLongDate ...
- charset
<meta charset="UTF-8" /> 这是html5的写法. <meta http-equiv=“content-type” content=“tex ...
- Orchard 源码探索(Localization)之国际化与本地化
本地化与国际化 基本上相关代码都在在Orchard.Framework.Localization中. T("english")是如何调用到WebViewPage.cs中的Local ...
- Copy an serializable object deeply
http://www.java2s.com/Tutorial/Java/0100__Class-Definition/Copyanserializableobjectdeeply.htm http:/ ...
- C语言入门(7)——自定义函数
C源程序是由函数组成的.虽然在C语言入门系列前面几篇的程序中大都只有一个主函数main(),但实用程序往往由多个函数组成.函数是C源程序的基本模块,通过对函数模块的调用实现特定的功能.C语言中的函数相 ...
- oracle查看所有表及字段
oracle表设计 http://blog.csdn.net/lanpy88/article/details/7580820 Oracle查看所有表和字段 获取表: select table_name ...
- Effective C++ Item 36 绝不又一次定义继承而来的 non-virtual 函数
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie 经验:绝对不要又一次定义继承而来的 non-virtual 函数 --> Item 7 ...
- Android Json生成及解析实例
JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...