By SmartPtr(http://www.shnenglu.com/SmartPtr/)
說起C++的模板及模板特化, 相信很多人都很熟悉 ,但是說到模板特化的幾種類型,相信了解的人就不是很多。我這里歸納了針對(duì)一個(gè)模板參數(shù)的類模板特化的幾種類型, 一是特化為絕對(duì)類型; 二是特化為引用,指針類型;三是特化為另外一個(gè)類模板。
這里用一個(gè)簡單的例子來說明這三種情況:
// general version
template<class T>
class Compare
{
public:
static bool IsEqual(const T& lh, const T& rh)
{
return lh == rh;
}
};
這是一個(gè)用于比較的類模板,里面可以有多種用于比較的函數(shù), 以IsEqual為例。
一、特化為絕對(duì)類型
也就是說直接為某個(gè)特定類型做特化,這是我們最常見的一種特化方式, 如特化為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;
}
};
二、特化為引用,指針類型
這種特化我最初是在stl源碼的的iterator_traits特化中發(fā)現(xiàn)的, 如下:
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;
};
當(dāng)然,除了T*, 我們也可以將T特化為 const T*, T&, const T&等,以下還是以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);
}
};
這種特化其實(shí)是就不是一種絕對(duì)的特化, 它只是對(duì)類型做了某些限定,但仍然保留了其一定的模板性,這種特化給我們提供了極大的方便, 如這里, 我們就不需要對(duì)int*, float*, double*等等類型分別做特化了。
三、特化為另外一個(gè)類模板
這其實(shí)是第二種方式的擴(kuò)展,其實(shí)也是對(duì)類型做了某種限定,而不是絕對(duì)化為某個(gè)具體類型,如下:
// 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的參數(shù)限定為一種vector類型, 但具體是vector<int>還是vector<float>, 我們可以不關(guān)心, 因?yàn)閷?duì)于這兩種類型,我們的處理方式是一樣的,我們可以把這種方式稱為“半特化”。
當(dāng)然, 我們可以將其“半特化”為任何我們自定義的模板類類型:
// 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);
}
};
這就是三種類型的模板特化, 我們可以這么使用這個(gè)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);
注:感謝longshanks對(duì)于本文中"類模板"與"模板類"概念的澄清,已更正 (2007-7-16)
posted on 2007-07-04 21:40
SmartPtr 閱讀(41393)
評(píng)論(13) 編輯 收藏 引用