C++类模板的三种特化
副标题#e#
说起C++的模板及模板特化,相信许多人都很熟悉,可是说到模板特化的几种范例,相信相识的人就不是许多。我这里归纳了模板特化的几种范例,一是特化为绝对范例;而是特化为引用,指针范例;三是特化为别的一个模板类。
这里用一个简朴的例子来说明这三种环境:
// general version
template<class T>
class Compare
{
public:
static bool IsEqual(const T& lh, const T& rh)
{
return lh == rh;
}
};
这是一个用于较量的模板类,内里可以有多种用于较量的函数,以IsEqual为例。
一、特化为绝对范例
也就是说直接为某个特定范例做特化,这是我们最常见的一种特化方法,如特化为float,double等
// specialize for float
template<>
class Compare<float>
{
public:
static bool IsEqual(const float& lh, const float& rh)
{
return abs(lh - rh) < 10e-3;
}
};
// specialize for double
template<>
class Compare<double>
{
public:
static bool IsEqual(const double& lh, const double& rh)
{
return abs(lh - rh) < 10e-6;
}
};
#p#副标题#e#
二、特化为引用,指针范例
这种特化我最初是在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;
};
// specialize for const _Tp*
template <class _Tp>
struct iterator_traits<const _Tp*> {
typedef random_access_iterator_tag iterator_category;
typedef _Tp value_type;
typedef ptrdiff_t difference_type;
typedef const _Tp* pointer;
typedef const _Tp& reference;
};
虽然,除了T*,我们也可以将T特化为constT*,T&,constT&等,以下照旧以T*为例:
// specialize for T*
template<class T>
class Compare<T*>
{
public:
static bool IsEqual(const T* lh, const T* rh)
{
return Compare<T>::IsEqual(*lh, *rh);
}
};
这种特化其实是就不是一种绝对的特化,它只是对范例做了某些限定,但仍然保存了其必然的模板性,这种特化给我们提供了极大的利便,如这里,我们就不需要对int*,float*,double*等等范例别离做特化了。
三、特化为别的一个模板类
这其实是第二种方法的扩展,其实也是对范例做了某种限定,而不是绝对化为某个详细范例,如下:
// specialize for vector<T>
template<class T>
class Compare<vector<T> >
{
public:
static bool IsEqual(const vector<T>& lh, const vector<T>& rh)
{
if(lh.size() != rh.size()) return false;
else
{
for(int i = 0; i < lh.size(); ++i)
{
if(lh[i] != rh[i]) return false;
}
}
return true;
}
};
这就把IsEqual的参数限定为一种vector范例,但详细是vector<int>照旧vector<float>,我们可以不体贴,因为对付这两种范例,我们的处理惩罚方法是一样的,我们可以把这种方法称为“半特化”。
虽然,我们可以将其“半特化”为任何我们自界说的模板类范例:
// 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)
{
return Compare<T>::IsEqual(lh.x1 + lh.x2, rh.x1 + rh.x2);
}
};
这就是三种范例的模板特化,我们可以这么利用这个Compare类:
// int
int i1 = 10;
int i2 = 10;
bool r1 = Compare<int>::IsEqual(i1, i2);
// float
float f1 = 10;
float f2 = 10;
bool r2 = Compare<float>::IsEqual(f1, f2);
// double
double d1 = 10;
double d2 = 10;
bool r3 = Compare<double>::IsEqual(d1, d2);
// pointer
int* p1 = &i1;
int* p2 = &i2;
bool r4 = Compare<int*>::IsEqual(p1, p2);
// vector<T>
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
vector<int> v2;
v2.push_back(1);
v2.push_back(2);
bool r5 = Compare<vector<int> >::IsEqual(v1, v2);
// 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);