// general version template<class T> class Compare { public: static bool IsEqual(const T& lh, const T& rh) { return lh == rh; } }; |
這是一個用于比較的類模板,里面可以有多種用于比較的函數(shù), 以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; } };
|
二、特化為引用,指針類型
這種特化我最初是在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; };
|
當然,除了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); } }; |
這種特化其實是就不是一種絕對的特化, 它只是對類型做了某些限定,但仍然保留了其一定的模板性,這種特化給我們提供了極大的方便, 如這里, 我們就不需要對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的參數(shù)限定為一種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);
|
模板有兩種特化,全特化和偏特化(局部特化)
模板函數(shù)只能全特化,沒有偏特化(以后可能有)。
模板類是可以全特化和偏特化的。
全特化,就是模板中模板參數(shù)全被指定為確定的類型。
全特化也就是定義了一個全新的類型,全特化的類中的函數(shù)可以與模板類不一樣。
偏特化,就是模板中的模板參數(shù)沒有被全部確定,需要編譯器在編譯時進行確定。
在類型上加上const、&、*( cosnt int、int&、int*、等等)并沒有產(chǎn)生新的類型。只是類型被修飾了。模板在編譯時,可以得到這些修飾信息。
模板的特化是非常有用的。它像一個在編譯期的條件判斷。當編譯器在編譯時找到了符合的特化實現(xiàn),就會使用這個特化實現(xiàn)。這就叫編譯器多態(tài)(或者叫靜態(tài)多態(tài))。這種東西對編寫基礎庫是很有用的。這也就是為何c++的基礎庫大量使用了模板技術,而且大量使用了特化,特別是偏特化。
在泛型中,利用特化類得到類新的特性,以便找到最適合這種特性的實現(xiàn)。而這一切都是在編譯時完成。