1. 程式人生 > >C++模板特化與偏特化

C++模板特化與偏特化

type 舉例 its 進一步 數據類型 類型 orm special template

C++模板

說到C++模板特化與偏特化,就不得不簡要的先說說C++中的模板。我們都知道,強類型的程序設計迫使我們為邏輯結構相同而具體數據類型不同的對象編寫模式一致的代碼,而無法抽取其中的共性,這樣顯然不利於程序的擴充和維護。C++模板就應運而生。C++的模板提供了對邏輯結構相同的數據對象通用行為的定義。這些模板運算對象的類型不是實際的數據類型,而是一種參數化的類型。C++中的模板分為類模板和函數模板。

類模板如下:


#include <iostream>
using namespace std;

template <class T>
class TClass
{
public:
// TClass的成員函數

private:
T DateMember;
};

函數模板如下:


template <class T>
T Max(const T a, const T b)
{
return a > b ? a : b;
}

模板特化

有時為了需要,針對特定的類型,需要對模板進行特化,也就是所謂的特殊處理。比如有以下的一段代碼:


#include <iostream>
using namespace std;

template <class T>
class TClass
{
public:
bool Equal(const T& arg, const T& arg1);
};

template <class T>
bool TClass<T>::Equal(const T& arg, const T& arg1)
{
return (arg == arg1);
}

int main()
{
TClass<int> obj;
cout<<obj.Equal(2, 2)<<endl;
cout<<obj.Equal(2, 4)<<endl;
}

類裏面就包括一個Equal方法,用來比較兩個參數是否相等;上面的代碼運行沒有任何問題;但是,你有沒有想過,在實際開發中是萬萬不能這樣寫的,對於float類型或者double的參數,絕對不能直接使用“==”符號進行判斷。所以,對於float或者double類型,我們需要進行特殊處理,處理如下:


#include <iostream>
using namespace std;

template <class T>
class Compare
{
public:
bool IsEqual(const T& arg, const T& arg1);
};

// 已經不具有template的意思了,已經明確為float了
template <>
class Compare<float>
{
public:
bool IsEqual(const float& arg, const float& arg1);
};

// 已經不具有template的意思了,已經明確為double了
template <>
class Compare<double>
{
public:
bool IsEqual(const double& arg, const double& arg1);
};

template <class T>
bool Compare<T>::IsEqual(const T& arg, const T& arg1)
{
cout<<"Call Compare<T>::IsEqual"<<endl;
return (arg == arg1);
}

bool Compare<float>::IsEqual(const float& arg, const float& arg1)
{
cout<<"Call Compare<float>::IsEqual"<<endl;
return (abs(arg - arg1) < 10e-3);
}

bool Compare<double>::IsEqual(const double& arg, const double& arg1)
{
cout<<"Call Compare<double>::IsEqual"<<endl;
return (abs(arg - arg1) < 10e-6);
}

int main()
{
Compare<int> obj;
Compare<float> obj1;
Compare<double> obj2;
cout<<obj.IsEqual(2, 2)<<endl;
cout<<obj1.IsEqual(2.003, 2.002)<<endl;
cout<<obj2.IsEqual(3.000002, 3.0000021)<<endl;
}

模板偏特化

上面對模板的特化進行了總結。那模板的偏特化呢?所謂的偏特化是指提供另一份template定義式,而其本身仍為templatized;也就是說,針對template參數更進一步的條件限制所設計出來的一個特化版本。這種偏特化的應用在STL中是隨處可見的。比如:


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;
};

看了了麽?這就是模板偏特化,與模板特化的區別在於,模板特化以後,實際上其本身已經不是templatized,而偏特化,仍然帶有templatized。我們來看一個實際的例子:


#include <iostream>
using namespace std;

// 一般化設計
template <class T, class T1>
class TestClass
{
public:
TestClass()
{
cout<<"T, T1"<<endl;
}
};

// 針對普通指針的偏特化設計
template <class T, class T1>
class TestClass<T*, T1*>
{
public:
TestClass()
{
cout<<"T*, T1*"<<endl;
}
};

// 針對const指針的偏特化設計
template <class T, class T1>
class TestClass<const T*, T1*>
{
public:
TestClass()
{
cout<<"const T*, T1*"<<endl;
}
};

int main()
{
TestClass<int, char> obj;
TestClass<int *, char *> obj1;
TestClass<const int *, char *> obj2;

return 0;
}

對於輸出結果,我這裏就不寫了,大家可以試一試。

特化與偏特化的調用順序

對於模板、模板的特化和模板的偏特化都存在的情況下,編譯器在編譯階段進行匹配時,是如何抉擇的呢?從哲學的角度來說,應該先照顧最特殊的,然後才是次特殊的,最後才是最普通的。編譯器進行抉擇也是尊從的這個道理。從上面的例子中,我們也可以看的出來,這就就不再舉例說明。

文章出自:http://m.jb51.net/show/56004


C++模板特化與偏特化