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++模板函数只能全特化不能偏特化
随机推荐
- QF——OC数组
OC中的数组: OC中的数组和它的字符串有很多相似之处.也分为可变和不可变. NSArray:不可变数组,一经初始化,便不能再更改: NSMutableArray:可变数组,它其实是继承于NSArra ...
- artTemplate-3.0
https://github.com/aui/artTemplate artTemplate-3.0 新一代 javascript 模板引擎 目录 特性 快速上手 模板语法 下载 方法 NodeJS ...
- codeforces 15D . Map 优先队列
题目链接 题目意思很简单nm的矩阵里, 选若干个ab的小矩阵, 定义每个矩阵的值为这个矩阵里的所有数的和-最小值*数的个数. 选小矩阵时, 优先选值最小的,然后次小的.. 知道不能选位置. 输出所有矩 ...
- echarts实现上海地域PM值(map、timeline)
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- GMTED2010 高程数据下载
http://topotools.cr.usgs.gov/GMTED_viewer/viewer.htm
- 解密电子书之四:MCU(freescale)
谈完国产的君正,让我们再看看呛了君正财路的freescale iMX51. 这是freescale近期的主打产品,用的是ARM Cortex A8架构,主频在消费电子领域最高可达800MHz,在工业领 ...
- rsyslog 日志格式和输出
日志格式: $EscapeControlCharactersOnReceive off #关闭rsyslog默认转译ASCII<32的所有怪异字符,包括换行符等 $template nginx- ...
- thanks使用注意事项;
router.get('/api/users/search/:key/:page', function(req, res) { if(_.isEmpty(req.params.key)) { res. ...
- JavaSE思维导图(三)
- matlab GUI之常用对话框(四)-- 输入对话框 inputdlg、目录对话框 uigetdir、列表对话框 listdlg
常用对话框(四) 1.输入对话框 inputdlg answer = inputdlg(prompt) answer = inputdlg(prompt,dlg_title) answer = in ...