Programming in C ++ template specialization only a member function (a member function specialization pattern) template class

Transfer: https://www.cnblogs.com/zhoug2020/p/6581477.html

 

To Laid template programming or partial specializations (partial specialization) a template class, the class template requires specialized all member functions. Most of the class template member function function may be exactly the same, we may only need to re-implement the member function or two when you can specialize. In this case, if a full rewrite all the member functions of the class template, will not only increase the workload, it is not conducive to the maintenance of the code.

For example, the following class template A, only the template parameter is char * when needed specialized member function func (), but other member functions do not require specialized:

Template. 1 <typename _Ty> 
 2 A struct 
 . 3 { 
 . 4 other member functions // A 
 . 5 // member functions of other B 
 . 6 // ...... 
 . 7 void FUNC () 
 . 8 { 
 . 9 STD :: COUT << "Common . type "STD :: << endl; 
10} 
. 11}; 
12 is 
13 is int main () 
14 { 
15 A <int> I; 
16 i.func (); 
. 17 
18 is A <char *> C; 
. 19 c.func (); 
20 is 
21 is return 0; 
22 is}

Method 1: The simplest runtime type identification, this method

Template. 1 <typename _Ty> 
 2 A struct 
 . 3 { 
 . 4 other member functions // A 
 . 5 // member functions of other B 
 . 6 // ...... 
 . 7 void FUNC () 
 . 8 { 
 . 9 IF (the typeid (_Ty) == the typeid (char *)) 
10 :: COUT << STD "Common type." << endl STD ::; 
. 11 the else 
12 is STD :: COUT << "Special type." :: STD << endl; 
13 is} 
14} ;

Method Two: The members of the class of functions to implement a template specialization, this method is relatively simple

Template. 1 <typename _Ty> 
 2 A struct 
 . 3 { 
 . 4 other member functions // A 
 . 5 // member functions of other B 
 . 6 // ...... 
 . 7 Template <typename __Ty> 
 . 8 void funcImpl () 
 . 9 { 
10 STD: : << COUT. "Common type" << endl STD ::; 
. 11} 
12 is 
13 is Template <> 
14 void funcImpl <char *> () 
15 { 
16 STD :: COUT << "Special type." << STD: : endl; 
. 17} 
18 is 
. 19 void FUNC () 
20 is { 
21 is funcImpl <_Ty> (); 
22 is} 
23 is};

Method three: by a nested achieved Laid template class

 1 template <typename _Ty>
 2 struct A
 3 {
 4     // 其他成员函数a
 5     // 其他成员函数b
 6     // ......
 7     template <typename __Ty>
 8     struct IsCharPType
 9     {
10         const static bool b = false;
11     };
12 
13     template<>
14     struct IsCharPType<char*>
15     {
16         const static bool b = true;
17     };
18 
19     void func()
20     {
21         if (IsCharPType<_Ty>::b)
22             std::cout << "special type." << std::endl;
23         else
24             std::cout << "common type." << std::endl;
25     }
26 };

Method four: define a nested class template, parameter types to achieve (by overriding the function of the different functions

 1 template <typename _Ty>
 2 struct A
 3 {
 4     // 其他成员函数a
 5     // 其他成员函数b
 6     // ......
 7     template <typename __Ty>
 8     struct TypeClass
 9     {
10     };
11 
12     template <typename __Ty>
13     void funcImpl(const TypeClass<__Ty>&)
14     {
15         std::cout << "common type." << std::endl;
16     }
17 
18     void funcImpl(const TypeClass<char*>&)
19     {
20         std::cout << "special type." << std::endl;
21     }
22 
23     void func()
24     {
25         funcImpl(TypeClass<_Ty>());
26     }
27 };

Guess you like

Origin www.cnblogs.com/dongzhiquan/p/cpp_template_class_specify_one_function.html