C ++ template details

Source: Internet
Author: User

C ++ templates fall into two categories:
1. Function Template
Format: template <typename form parameter name, typename form Parameter Name> Reverse type function name (parameter list) {function body}
For example:
Template <typename T> void swap (T & a, T & B ){}
 
Call time:
Int a, B;
Swap (a, B );
 
Double d1, d2;
Swap (d1, d2 );
 
However, the following statements are incorrect:
Swap (int, int)
 
2. class template
Format: template <typename parameter name, typename parameter name...> Class Name {}
 
For example:
 
 
Cpp Code
1. template <typename T>
2. class
3 .{
4. public:
5. T;
6. T B;
7. T hy (T c, T & d );
8 .};
Create A class template object: if there is A template class A, use the class template to create an object as A <int> m; after Class A, follow A <> angle bracket
 
And fill in the corresponding type in it. In this case, any part of Class A that uses the template parameters will be replaced by int. When a class template has two template parameters
The method used to create an object is A <int, double> m. The types are separated by commas.
 
 
For a class template, the type of the template parameter must be explicitly specified in the angle brackets after the class name. For example, A <2> m. Use this method to set the template parameter to int.
Yes. The parameter of the class template does not have the question of real parameter deduction. That is to say, the integer value 2 cannot be deduced to the int type and passed to the template parameter. Class Template
When the parameter is set to int type, A <int> m must be specified.
 
The method for defining a member function externally in the class template is: template <template form parameter list> function Reverse type class name <template form Parameter Name>: function name (parameter list) {function body }, for example, if there are two template parameters T1 and class A of T2 contains A void h () function, the syntax of this function is defined as: template <class
T1, class T2> void A <T1, T2 >:: h (){}. Note: When a class member is defined outside the class, the template parameters following the template should correspond to the modulo of the class to be defined.
The plate parameters are consistent.
 
Template parameters
 
There are three types of template parameters: type parameters, non-type parameters, and template parameters.
1. type parameters
1.1 type template parameters: the type parameters are composed of class or typename followed by specifiers, such as template <class T> void h (T ){}; T is a type parameter, and the name of the type parameter is determined by the user. The template parameter indicates an unknown type. Template type parameters can be used as types
The identifier is used anywhere in the template. It is used exactly the same as the built-in type identifier or Class Identifier, that is, it can be used to specify the Reverse type,
Variable declaration.
1.2 two different types cannot be specified for the same template type parameter, such as template <class T> void h (T a, T B) {}, The statement calls h (2, 3.2) an error occurs because the statement specifies two types for the same template parameter T. The first real parameter 2 specifies the template parameter T as int, the second real parameter 3.2 specifies the template parameter as double. If the two types of parameters are inconsistent, an error occurs.
2. Non-type parameters
2.1 Non-type template parameters: the non-type parameters of the template are also built-in type parameters, such as template <class T, int a> class B {}; where int a is non-type
Template parameters.
2.2 A non-type parameter is a constant value within the template definition, that is, a non-type parameter is a constant within the template.
2.3 non-template type parameters can only be integer, pointer, and reference, such as double, String, String ** type is not allowed. But double &, double *, the object reference or pointer is correct.
2.4 The real parameter that calls a non-type template parameter must be a constant expression, that is, it must be able to calculate the result within the compile time.
2.5 note: the address of any local object, local variable, local object, or local variable is not a constant expression and cannot be used as a non-type model.
The real parameters of the plate parameters. The global pointer type, global variable, and global object are not a constant expression and cannot be used as real parameters of non-type template parameters.
2.6 The address or reference of a global variable. The address of a global object or the reference of a const type variable is a constant expression and can be used as a real parameter of a non-type template parameter.
 
 
 
Example of non-type parameters in a class template
// The template declaration or definition can only be performed within the global, namespace, or class range. That is, a template cannot be declared or defined in the main function.
 
Cpp Code
1. // class template Definition
2. template <class T> class A {public: T g (T a, T B); A () ;}; // defines class A with A class template type parameter T
3. template <class T1, class T2> class B {public: void g () ;}; // defines class B with two class template type parameters T1 and T2
 
 
Cpp Code
1. // define the default type parameters of the class template. The default type parameters are not suitable for function templates.
2. template <class T1, class T2 = int> class D {public: void g () ;}; // defines a class template with default type parameters. Here, T2 is set to int by default.
Cpp Code
1. // template <class T1 = int, class T2> class E {}; // error, if the parameter of the default type is set for T1, all the parameters after T1 must be set with the default value.
 
 
Cpp Code
1. // The following is the definition of a non-type parameter www.2cto.com
2. // a non-type parameter can only be an integer, pointer, or reference. A type such as double, String, or String ** is not allowed. But the double &, double * object references or refers
3. The needle is correct.
4. template <class T1, int a> class Ci {public: void g () ;}; // defines the non-type parameters of the template. The parameter is an integer type.
5. template <class T1, int & a> class Cip {public: void g ();};
6. template <class T1, A <int> * m> class Cc {public: void g () ;}; // defines the template type parameters of the template, pointer to an object of Class A with an int parameter.
7. template <class T1, double * a> class Cd {public: void g () ;}; // defines the non-type parameters of the template, which are referenced by the double type.
8. class E {}; template <class T1, E & m> class Ce {}; // non-type template parameters are references of objects.
9. // The following non-type parameter declaration is incorrect.
10. // template <class T1, A m> class Cc {}; // error. The object cannot be A non-type parameter. The type of A non-type template parameter can only be A reference or pointer to an object.
11. // template <class T1, double a> class Cc {}; // error. The parameter of a non-type template cannot be of the double type, but can be a reference of the double type.
12. // template <class T1, A <int> m> class Cc {}; // error. The parameter of A non-type template cannot be an object and must be an object reference or pointer. This rule applies to template parameters.
13. No exception.
14. // method for defining members of various types outside the class template,
15. // typeid (variable name ). name () is used to extract the type of the variable name. For example, if int a is used, cout <typeid (). name () will output int
16. template <class T> A <T>: A () {cout <"class A goucao" <typeid (T ). name () <endl;} // method of defining the constructor of the class outside the class template
17. template <class T> t a <T>: g (T a, T B) {cout <"class A g (T a, T B) "<endl;} // define a member of the class template outside the class template
18. template <class T1, class T2> void B <T1, T2>: g () {cout <"class g f ()" <typeid (T1 ). name () <typeid (T2 ). name () <endl ;}
19. // when a class member is defined outside the class, the template parameters following the template should be consistent with the template parameters of the class to be defined
20. template <class T1, int a> void Ci <T1, a >:: g () {cout <"class Ci g ()" <typeid (T1 ). name () <endl ;}
21. template <class T1, int & a> void Cip <T1, a >:: g () {cout <"class Cip g ()" <typeid (T1 ). name () <endl ;}
22. // when a member of the class's external definition class, the template parameters after the template should be consistent with the template parameters of the class to be defined
23. template <class T1, A <int> * m> void Cc <T1, m >:: g () {cout <"class Cc g () "<typeid (T1 ). name () <endl ;}
24. template <class T1, double * a> void Cd <T1, a >:: g () {cout <"class Cd g ()" <typeid (T1 ). name () <endl ;}
25. // a template class with a default type of parameters. The method of defining members outside the class is as follows.
26. // when a member of the class's external definition class is involved, the default value in the form parameter table of the template should be omitted
27. template <class T1, class T2> void D <T1, T2 >:: g () {cout <"class D g ()" <endl ;}
28. // template <class T1, class T2 = int> void D <T1, T2 >:: g () {cout <"class D k ()" <endl ;} // error. The default type is defined outside the class template.
29. The default value in the form parameter table of the template should be omitted.
30. // define some global variables.
31. int e = 2; double ed = 2.2; double * pe = & ed;
32. A <int> mw; A <int> * pec = & mw; E me;
33. // start the main function
34. int main ()
35. {// template <class T> void h () {} // error. The template declaration or definition can only be performed globally, in The namespace or within the scope of the class. That is, it cannot be performed in a local range within the function.
36. // A <2> m; // error. There is no real parameter deduction problem for the class template. The class template must specify its type in angle brackets.
37. // class template call instance
38. A <int> ma; // output "class A goucao int" to create the ma object of class template A of the int type.
39. B <int, int> mb; mb. g (); // output "class B g () int" to create the object mb of class template B, and design the type parameters T1 and T2 as int
40. // call a non-type parameter
41. // The real parameter that calls a non-type template parameter must be a constant expression, that is, it must be able to calculate the result during compilation. Any local object, local variable, local object address, local
42. The addresses of variables are not a constant expression and cannot be used as real parameters of non-type template parameters. Global pointer type, global variable, global object is not a constant expression, cannot
43. Used as a real parameter for a non-type template parameter.
44. // address or reference of the global variable. The address of the global object or reference a const type variable is a constant expression and can be used as a real parameter of a non-type template parameter.
45. // call the method of the int-type non-type parameter to name the Ci parameter. The declaration form is template <class T1, int a> class Ci.
46. Ci <int, GHIJKLMJKLNOPQMII // correct. The value R is an int type constant and outputs "class Ci g () int"
47. const int a2 = SMITLUint, a2> mci1; mci1.g (); // correct, because a2 is a constant of the const type here. Output "class Ci g () int"
48. // Ci <int, a> mci; // error. int-type variable a is a local variable, not a constant expression.
49. // Ci <int, e> mci; // incorrect. The Global int variable e is not a constant expression.
50. // call the method class name of the int & type non-type parameter Cip. The declaration form is template <class T1, int & a> class Cip.
51. Cip <int, e> mcip; // correct. The reference or address of the global variable is a constant expression.
52. // Cip <int, a> mcip1; // error. The reference or address of a local variable is not a constant expression.
53. // call the non-class parameter name Cd of the double * type. The declaration form is template <class T1, double * a> class Cd.
54. Cd <int, & ed> mcd; // correct. The reference or address of the global variable is a constant expression.
55. // Cd <int, pe> mcd1; // error. The global variable pointer is not a constant expression.
56. // double dd = aNGMIITbULcdefbbHIJKbgMIhh error. The address of the local variable is not a constant expression and cannot be used as a real parameter of a non-type parameter.
57. // Cd <int, & e> mcd; // error. Although some conversions are allowed for non-type parameters, this conversion cannot be implemented.
58. // call the method class name Cc of the template type parameter object A <int> * and the method class name is template <class T1, A <int> * m> class Cc
59. Cc <int, & mw> mcc; mcc. g (); // correct. The Global object address or reference is a constant expression.
60. // Cc <int, & ma> mcc; // error. The address or reference of a local variable is not a constant expression.
61. // Cc <int, pec> mcc2; // error. The Global Object Pointer is not a constant expression.
62. // call the method class referenced by the non-type parameter E & object named Ce. Declaration Form: template <class T1, E & m> class Ce
63. E me1; // Ce <int, me1> mce1; // error. The local object is not a constant expression.
64. Ce <int, me> mce; // correct. The pointer or reference of a global object is a constant expression.
65. // non-type conversion example, Class Name: Ci
66. // non-type parameters can be converted from arrays to pointers, from functions to pointers, const modifier conversion, lift conversion, Integer Conversion, and regular conversion.
67. const short s = „ M Ci <int, s> mci... MI sort // correct. Although the short and int types do not exactly match, the short type can be converted to the int type here.
 
 
Cpp Code
1. // real parameter deduction example of the function template.
2. // h (int); // error. For a function template, a call such as h (int, int) does not exist. You cannot specify the type of the template parameter in the parameter of the function call, call a function Template
3. Real parameter deduction should be used, that is, only h (2,©Call such as Q, or int a, B; h (a, B ).
4. // h function form: template <class T> void h (T)
5. h (2); // The output "hansu h () int" is deduced using the function template. Here, the value 2 is int, so the type parameter T is deduced as int.
6. h (2. Sort QMI // output "hansu h () double", because 2.®Is double type, so the type parameter of the function template is deduced as double type
7. // k Function Form: template <class T> void k (T a, T B)
8. k (2, ° QMI // output "hansu k () int"
9. // k (2, ± N random QMIhh error, the type of the template parameter T is not clear, because the first parameter type of k () function is int, the second is double, the two parameters are of different types.
10. // f function form: template <class T1, class T2> void f (T1 a, T2 B)
11. f (¶ e... N ¬ QMI // output "hansu f () int, double". There is no error in the deduction of template parameters because the template function has two types of parameters T1 and T2. Here we will push T1
12. Assume It is an int, and T2 is deduced as a double.
13. int a = random Mdouble B = random M
14. f (a, B); // output is the same as above. Here, the variable name is used to deduct the real parameters of the plate.
15. // conversion example allowed by the template function deduction. The form of the g function is template <class T> void g (const T *)
16. int a1 áâ à äå ge  † M g (a1); // output "hansu g () int". The address of the array and the parameter const T * do not exactly match, therefore, the a1 address T & is converted to const T *, while the a1 address
17. It is int type, so T is deduced as int at last.
18.g (& B); // output "hansu g () double", which is the same as the preceding one, but converts the type T to the double type.
19. h (& B) ;}// output "hansu h () double *" here, the model parameter type T is deduced as double.
 
Cpp Code
1. <SPAN style = "WHITE-SPACE: normal">
2. </SPAN>
 

 

From jiangwenfeng762

Related Article

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.