C + + template programming in the main version of the template class, full-special, partial-Special (c + + Type Traits)

Source: Internet
Author: User
Tags traits

1. Main version template class

First we look at a beginner can understand, the application of the template program:

1 #include <iostream> 2 using namespace std; 3  4 Template<class T1, class T2> 5 class a{6 public:7     void function (T1 value1, T2 value2) {8         cout<& lt; " value1 = "<<value1<<endl; 9         cout<< "value2 =" <<value2<<endl;10     }11};12 int main () {     a<int, char> a;14     a.function (' B ');     system ("pause"); +     return 0;18}

The program is simple, a template Class A, which has a function to print two parameters. We created the object of Class A with the Int,char type in the main function, and after calling the function, we got the result we wanted:

value1 = 12value2 = B Press any key to continue ...

The above template Class A, commonly referred to as the main version of the template class , is characterized by the following: All types in the template class are template types.

2. Full specificity

First of all we need to understand a concept, what is called specificity. Specialization is actually a special meaning, in the template class, all types are templates (Template<class t>), and once we have all the template type T are explicit, and write a class name and the main template class name the same class, then this class is called the full specialization class . The following code is a demonstration of full specificity:

1 #include <iostream> 2 using namespace std; 3  4 Template<class T1, class T2> 5 class a{6 public:7     void function (T1 value1, T2 value2) {8         cout<& lt; " value1 = "<<value1<<endl; 9         cout<< "value2 =" <<value2<<endl;10     }11};12 class template<>14, double >{//Type explicit, is a fully-specific class,     public:16     void function (int value1, double value2) {         cout<< "int value1 =" <<value1<<endl;18         cout<< "Double value2 =" <<value2<<endl;19     }20};21 int Main () {     a<int, double> a;24     a.function (12.21);     system ("pause");     return 0;28}

The part that is shaded in the above code is a fully-specific class A, and you can see that the T1 and T2 in the main version of the template class have all been explicitly converted to int and double. Then in main use "A<int, double> A;" When instantiating an object, the program invokes the fully-specific class. A class is referred to as the condition of a fully-specific class: 1. You must have a master template Class 2. The template type is fully defined.

3. Partial localization

The main version template class and the full-special class are defined above, then the bias is a template class between the two, its class name is the same as the main version template class, but its template type, there are the explicit part and the part is not explicit. Here is a partial demonstration code:

1 #include <iostream> 2 using namespace std; 3  4 Template<class T1, class T2> 5 class a{6 public:7     void function (T1 value1, T2 value2) {8         cout<& lt; " value1 = "<<value1<<endl; 9         cout<< "value2 =" <<value2<<endl;10     }11};12, Template<class t>14 class A<int, T& Gt {//Type partial explicit, is a biased class of public:16     void function (int value1, T value2) {         cout<< "int value1 =" <<value1 <<endl;18         cout<< "Double value2 =" <<value2<<endl;19     }20};21 int main () {%     A <int, char> a;24     a.function (n, ' a ');     system ("pause");     return 0;28}

The above Code coloring section is a biased template class, you can see the main version of the template class T1 and T2 in the T1 is explicitly converted to int. Then in main use "A<int, char> A;" When instantiating an object, the program will call this biased. A class is referred to as the condition of a partial class: 1. You must have a master template Class 2. The template type is partially clarified.

4. template class Call Priority

The priority of calling priorities from high to low for the main version of the template class, the full-scale class, and the superclass class is: full-Special class > Partial class > Main version template class. This order of precedence is best for performance.

5. Other types of specificity

At the time of the special, we will make the template type in the main version template class special, according to the type of special, we can classify:

①. Absolute type of specificity

②. Reference/pointer type specificity

③. Convert to a different class template

Let's take a look at these three types of specificity:

①. Absolute type of specificity

The so-called absolute type specificity is the template type T is specific to the developed normal data types or custom data types. The code is as follows:

1 class mytype{//Custom type 2 public:3     char ch; 4     MyType (char _ch): Ch (_ch) {} 5}; 6  7 Template<class T1, Clas S t2> 8 class a{9 public:10     void function (T1 value1, T2 value2) {One         cout<< "value1 =" <<value1<&l t;endl;12         cout<< "value2 =" <<value2<<endl;13     }14};15, template<>17 class A<int, mytype>{//absolute type specificity, two template types are special to a generic type and a custom data type of public:19     void function (int value1, MyType value2) {         cout<< "int value1 =" <<value1<<endl;21         cout<< "Double value2 =" <<value2.ch<< Endl;22     }23};

②. Reference/pointer type specificity

1 Template<class T1, Class T2> 2 class a{3 public:4     void function (T1 value1, T2 value2) {5         cout<< "va lue1 = "<<value1<<endl; 6         cout<< "value2 =" <<value2<<endl; 7     } 8}; 9 Template<class T1, Class t2>11 class a<t1*, t2*>{//pointer type special public:13     void function (t1* pValue1, T2 * pValue2) {         cout<< "int value1 =" <<*pvalue1<<endl;15         cout<< "Double value2 =" < <*pvalue2<<endl;16     }17};18 template<class T1, Class t2>20 class A<t1&, t2&>{//reference type Special public:22     void function (t1& pValue1, t2& pValue2) {cout<<         "int value1 =" <<pvalue1 <<endl;24         cout<< "Double value2 =" <<pvalue2<<endl;25     }26};

③. Convert to a different class template

1 Template<class t> 2 class number{3 public:4     t value; 5 number     (T V): value (v) {} 6}; 7  8 Template<c Lass T1, class T2> 9 class a{10 public:11     void function (T1 value1, T2 value2) {cout<<         "value1 =" << ; value1<<endl;13         cout<< "value2 =" <<value2<<endl;14     }15};16 template<class T1 , Class t2>18 class A<number<t1>, number<t2>>{//template type becomes another template class, public:20     void function ( Number<t1> N1, number<t2> n2) {cout<<         "int value1 =" <<n1.value<<endl;22         cout << "Double value2 =" <<n2.value<<endl;23     }24};

6. Conclusion

Of course, this article is only superficial to let the reader understand what is the template of the special, in depth, this article is only to touch the fur, template of the subtle skills of this article is not covered, please look forward to.

C + + template programming in the main version of the template class, full-special, partial-Special (c + + Type Traits)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.