C++模板ios
說到C++模板特化與偏特化,就不得不簡要的先說說C++中的模板。咱們都知道,強類型的程序設計迫使咱們爲邏輯結構相同而具體數據類型不一樣的對象編寫模式一致的代碼,而沒法抽取其中的共性,這樣顯然不利於程序的擴充和維護。C++模板就應運而生。C++的模板提供了對邏輯結構相同的數據對象通用行爲的定義。這些模板運算對象的類型不是實際的數據類型,而是一種參數化的類型。C++中的模板分爲類模板和函數模板。dom
類模板以下:函數
#include <iostream>
using namespace std;
template <class T>
class TClass
{
public:
// TClass的成員函數
private:
T DateMember;
};
函數模板以下:spa
template <class T>
T Max(const T a, const T b)
{
return a > b ? a : b;
}
模板特化.net
有時爲了須要,針對特定的類型,須要對模板進行特化,也就是所謂的特殊處理。好比有如下的一段代碼:設計
#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;
}
模板偏特化code
上面對模板的特化進行了總結。那模板的偏特化呢?所謂的偏特化是指提供另外一份template定義式,而其自己仍爲templatized;也就是說,針對template參數更進一步的條件限制所設計出來的一個特化版本。這種偏特化的應用在STL中是隨處可見的。好比:orm
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