程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++類模板的三種特化類型

C++類模板的三種特化類型

編輯:C++入門知識

說起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; } };         二、特化為引用,指針類型 這種特化我最初是在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特化為 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的參數限定為一種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);  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved