类模板的全特化与偏特化
类模板
template<typename T1, typename T2> class Test { public: Test(T1 i,T2 j):a(i),b(j){cout<<"模板类"<<endl;} private: T1 a; T2 b; };
全特化
template<> class Test<int , char> { public: Test(int i, char j):a(i),b(j){cout<<"全特化"<<endl;} private: int a; char b; };
偏特化
template <typename T2> class Test<char, T2> { public: Test(char i, T2 j):a(i),b(j){cout<<"偏特化"<<endl;} private: char a; T2 b; };
在具现化时,根据最相近的模板进行实例化,即,存在特化模板时,按照特化模板实例化,如果没有,再去按照模板进行实例化
依次调用类模板、全特化与偏特化
Test<double , double> t1(0.1,0.2);
Test<int , char> t2(1,'A');
Test<char, bool> t3('A',true);