C + + class template depth profiling

Source: Internet
Author: User

1. The generic type of a class template can define multiple template <typename t1, typename t2>class test{public:void  Add (t1 a, t2, b);}; When used:  test<int, float> t1;//t1 refers to the type of int, t2 is the generic type is float. 2. The type of the class template (1) means that if a class template is defined, there are multiple parameters for this class template, but if we pass the parameter with the same type of argument when we use the class template, the compiler will make the class template special to a class template of the parameter. (2) The special type of template is divided into: partial special, and completely special. Partial specificity: Use specific rules to constrain type parameters, some type parameters must be specified, and implement class templates separately according to type parameters such as: after template template< typename t1, typname  t2 >< typename t >class testclass test <t, t>// Constraints for partial specificity {{};}; When the type parameter is different, the left-hand class template is selected, when the type parameter is the same. Class test< t, t> is the constraint completely special: The full display of the specified type parameters such as: Template Special after the TEMPLATE&LT;&NBSP;TYPENAME&NBSP;T1, When  typname t2 ><  >//is fully special, it is not necessary to declare a generic type class testclass test < int, int> //This is the full specificity of {{};}; When using the class template, and specifying that all the type parameters are the same, such as int, the compiler will make the left class template special to the right class template, there is no generic type in the class template. When the type parameter is different, the class template on the left is used. (3) According to the experimental example, the Special class template is the program with a left-hand class template (This class templatehas two generic types), but at the same time we define a class template with the same name, but the two generic types of this class template are the same. This time, when we use this class template in the program, if the parameter type is not the same, the compiler will use the left one, if the parameter type is the same, the compiler will use the right one. The compiler will assume that the class template on the right is a special kind of class template on the left, and the compiler will not assume that the class template on the left is a new class template, just a special of the class template on the left, so the compilation can pass. Summary: A class template, according to the different types of parameters of the implementation of this class template, in fact, is a class template of the special process. Example: #include  <iostream>using namespace std;template < typename T1,  Typename t2 >class test{public:void add (t1 a, t2 b) {cout <<   "Void add (t1 a, t2 b)"  << endl;cout << a +  b << endl;}};/ Part of the special *************************************************/ template< typename t>class test < t, t >//, this place has a binding condition, Although this class template has the same name as the class template above, the compiler will not assume that the class template is a new class template, but rather a special form of the above class template, {///When we use the test class template, if the specified parameter type is different, the compiler will use the above class template implementation, If you specify the same parameter type, the compiler will use the implementation of this class template Public:void add (t a, t b) {COUT&Nbsp;<<  "Void add (t a, t b)"  << endl;cout << a  + b << endl;} Void print (void)//Even if more than one print member function, compile is passed, so is to support this special way of {cout <<  "class test  < T, T > " << endl;}};/ Fully Special ***************************************************** /template< >//the full specificity of a class template, it is not necessary to make a generic type declaration class test <void *, void * >//This implementation is used when the test class template is used when the parameter type is void *. {Public:void add (void * a, void * b) {cout <<  "Void add (void &NBSP;*&NBSP;A,&NBSP;VOID&NBSP;*&NBSP;B) " << endl;cout << " Error run  not to add because type is void * ... " << endl;}};/ The test class template is specific to a case where two parameters are pointers respectively **********************************/template < typename t1, typename t2>class test <  t1 *, t2 *>{public:void add (t1 *a, t2 *b) {cout <<  "Void add (t1 *a, t2 *b)"  << endl;cout << *a + *b  << endl;}}; Int main (void) {int a = 1;double b = 1.0; test<int, float> t1;//uses a test class template that is not special test<long, long> t2;// Using a special test class template test<void *, void *> t3; Test<int *, double *> t4;t1.add (2, 2.5); T2.add (10, 10); T2.print (); T3.add ( &AMP;A,&NBSP;&AMP;B); T4.add (&a, &b); return 0;} 3. Continue to understand the special type template (1) The special type of template is based on the need to separate a class template to implement. Specificity is just a separate implementation of the template, essentially the same class template. The use of a special class template is uniform, which means that each type parameter must be displayed. (2) Question: Is there a difference between class template specificity and redefinition? A: There are differences, redefinition and specificity are different. In essence, if you redefine a class template, either you implement a new class or you end up with two class templates. Essentially different. Use of the time can not be used uniformly, in the use of we have to consider which to choose. The essence of specificity is that only the same class template is implemented, but this classTemplates are implemented separately, which is essentially different. The use of class templates and special classes is done in a uniform way, because essentially a class template is implemented, and when used, the compiler chooses which class template or special class to use depending on the parameter type. So the specificity of the class template is to separate a class template to achieve, this sentence is very important. (3) Question: Can function templates be customized? A: The function template only supports the full specificity of the type parameter, and does not support partial specificity, that is, the specified parameter type that is displayed after the function name. such as: full customization of function template Template< typename t >bool equal (t a, t b)//definition of function template {return  a == b;} Template< >bool equal<void *> (void *, void *)//full specificity of function template {return  a == b;} (4) Proposals in the project: when the need to overload the function template, we have to prioritize the use of template-specific way to implement a function template, instead of overloading the function template to the new implementation of a function, when the template is not specific to meet the requirements, when the use of function overloading. Template specificity is used in engineering instead of Class (function) redefinition. Example: Partial specificity of class templates, full specificity, full specificity of function templates. Preference is given to the way in which a template can be redefined to add functionality when it is needed. #include  <iostream> #include  <string>using namespace std;/** to implement a class template in a way that The essence is to separate the implementation of the class template **/template<typename t1, typename t2> class why{public:void  Print (t1 a, t2 b) {cout <<  "Void print (t1 a, t2 b)"   << endl;cout << a <<  " "  << b << endl;}}; template<typename t>class why<t, t>//is part of the class template, {Public:void print (T a, &NBSP;T&NBSP;B) {cout <<  "Void print (t a, t b)"  << endl; cout << a <<  " "  << b << endl;}}; template< >class why<int, int>//the full specificity of the class template {Public:void print (int a,  INT&NBSP;B) {cout <<  "Void print (int a, int b)"  << endl; cout << a <<  " "  << b << endl;}};/ * * The specificity of the function template only supports full specificity * implementation of a function template, and fully special, that is, the implementation of a function template, which is called the special. */template <typename t>bool equal (t a, t b) {return a == b;} template< >//function template is completely special, the reason to special this function template is because, the comparison of floating-point numbers is not pure only with = = to compare. Bool equal<double> (double a, double b) {Const double delta = 0.00000000001;double r = a - b;cout < <  "bool equal<double> (double a, double b)"  << endl;return   ((-delta < r)  &&  (R < delta));  }/** Overloaded equal functions to achieve the method of comparing floating-point numbers, but this approach is not a priority, because this way, in the use of the time to consider how to choose, to give priority to the use of special methods. * When you can use a special method, you do not consider the way of using this function overload, regardless of the class template or function template, it is preferred to use a special way to separate implementation, because this is to implement a class template or function template. */bool equal (double a, double b) {const double delta = 0.00000000001; double r = a - b;cout <<  "Bool equal (double a, double &NBSP;B) " << endl;return  (-delta < r)  &&  (r <  delta));} Int main (void) {double a = 0.0, b = 0.0; why<int, double> w1; why<string, string> w2; Why<int, int>w3;w2.print ("Why",  "FANgqingqing "); W1.print (1, 2.2); W3.print (100,100);cout << " please input two  Double number ... " << endl;cin >> a >> b;cout < < Equal<double> (a, b)  << endl;cin >> a >> b ; cout << equal (A, b)  << endl;return 0;}


C + + class template depth profiling

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.