(1)特化为绝对类型:即直接为某个特定类型做特化。
下面给出代码实例:
template<typename T> class Test { public: Test(T t = 0):data(t) { cout << "Test object!" << endl; } private: T data; }; template<class T> class Compare { public: static bool IsEqual(const T& lh,const T& rh) { cout << "Compare<T>!" << endl; return lh == rh; } }; template<> class Compare<float> { public: static bool IsEqual(const float& lh,const float& rh) { cout << "Compare<float>!" << endl; return abs(lh - rh) < 10e-3; } }; template<> class Compare<double> { public: static bool IsEqual(const double& lh,const double& rh) { cout << "Compare<double>!" << endl; return abs(lh - rh) < 10e-6; } }; int main(int argc,char**argv) { Compare<int> a; a.IsEqual(5,3); Compare<float> b; b.IsEqual(5.1f,3.3f); Compare<double> c; c.IsEqual(5.1,3.3); return 0; }
(2)特化为引用,指针类型。
template<class T> class Compare { public: static bool IsEqual(const T& lh,const T& rh) { cout << "Compare<T>!" << endl; return lh == rh; } }; template<> class Compare<int> { public: static bool IsEqual(const int& lh,const int& rh) { cout << "Compare<int>!" << endl; return lh == rh; } }; template<class T> class Compare<T&> { public: static bool IsEqual(const T& lh,const T& rh) { cout << "Compare<T&>!" << endl; return Compare<T>::IsEqual(lh,rh); } }; template<class T> class Compare<T*> { public: static bool IsEqual(const T* lh,const T* rh) { cout << "Compare<T*>!" << endl; return Compare<T>::IsEqual(*lh,*rh); } }; int main(int argc,char**argv) { int * p1 = new int(10); int * p2 = new int(20); Compare<int*> c; c.IsEqual(p1,p2); int a = 10; int b = 3; int &a1 = a; int &b1 = b; Compare<int&> d; d.IsEqual(a1,b1); return 0; }
除了特化为T*,我们也可以转化为const T*,T&,const T&。
(3)转化为另一个类的模板
template<class T> class Test { public: Test(T t = T()):data(t) { cout << "Test object!" << endl; } public: bool operator==(const T& t) { return this->data == t.data; } T get_data()const { return data; } private: T data; }; template<class T> class Compare { public: static bool IsEqual(const T& lh,const T& rh) { cout << "Compare<T>!" << endl; return lh == rh; } }; template<class T> class Compare<vector<T> > { public: static bool IsEqual(const vector<T>& lh,const vector<T>& rh) { cout << "Compare<vector<T> >!"<< endl; 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; } }; template<class T> class Compare<Test<T> > { public: static bool IsEqual(const Test<T>& lh,const Test<T>& rh) { cout << "Compare<Test<T> >!" << endl; return lh.get_data() == rh.get_data(); } }; int main(int argc,char**argv) { Test<int> a(10); Test<int> b(15); Compare<Test<int> > c; c.IsEqual(a,b); return 0; } #endif
以上就是C++类模板特化的三种形式。。。。
我们下面来看类模板成员函数的特化:
//类模板成员函数的特化 class Test { public: Test() { cout << "Test()!" << endl; } }; class Sample2 { public: template<class T> void print() { cout << "Sample2 print template!" << endl; } }; template<> void Sample2::print<double>() { cout << "Sample2 print char!" << endl; } template<> void Sample2::print<int>() { cout << "Sample2 print int!" << endl; } template<> void Sample2::print<char>() { cout << "Sample2 print char!" << endl; } template<> void Sample2::print<Test>() { cout << "Sample2 print Test!" << endl; } int main(int argc,char**argv) { Sample2 sam2; sam2.print<double>(); sam2.print<int>(); sam2.print<char>(); sam2.print<Test>(); return 0; }
类的成员模板函数的特化:
//类的成员模板函数的特化 class Test { public: Test() { cout << "Test()!" << endl; } }; template<class T> class Sample { public: void print() { cout << "print template!" << endl; } }; template<> void Sample<int>::print() { cout << "print int!" << endl; } template<> void Sample<Test>::print() { cout << "print Test!" << endl; } int main(int argc,char**argv) { Sample<int> a; a.print(); Sample<double> b; b.print(); Sample<Test> c; c.print(); return 0; }