16.1.2 define a class template
Template <class Type>
Class Queue {
Public:
Queue ();
Type & front ();
Const Type & front () const;
Void push (const Type &);
Void pop ();
Bool empty () const;
Private:
};
Template <class Type>
Class Queue {
Public:
Queue ();
Type & front ();
Const Type & front () const;
Void push (const Type &);
Void pop ();
Bool empty () const;
Private:
}; The class template is also a template, so it must begin with the keyword template, followed by the template parameter table. The Queue template accepts a template Type parameter named Type.
Apart from the template parameter table, the class template definition looks similar to any other class. A class template can define data members, function members, and type members, use access labels to control access to members, and define constructors and destructor. In the definition of classes and class members, you can use template parameters as placeholders of types or values, and provide those types or values when using classes.
Compared with calling a function template, when using a class template, you must explicitly specify the real parameters for the template parameters.
Queue <int> iq;
Queue <vector <int> vq;
Queue <int> iq;
Queue <vector <int> vq; the compiler uses real parameters to instantiate a specific version of this class.
16.1.3 template parameters
Like function parameters, the names selected by programmers for template parameters have no essential meaning. The name of the template parameter can be T, but it can also be named as any name.
The only meaning that a template parameter can be assigned is whether it is a type parameter or a non-type parameter. If it is a type parameter, we know that this parameter represents an unknown type. If it is a non-type parameter, we know that it is an unknown value.
If you want to use the type or value represented by the template parameters, you can use the same name as the corresponding template parameters.
1. Scope of template parameters
The name of the template parameter can be used after it is declared as a template parameter until the end of the template declaration or definition.
The template parameters follow the regular name blocking rules. A template parameter with the same name as an object, function, or type declared in the global scope shields the global name.
Typedef double T;
Template <typename T> inline // global T was hided.
Const T & the_min (const T & v1, const T & v2 ){
If (compare (v1, v2) =-1)
Return v1;
Else
Return v2;
}
Typedef double T;
Template <typename T> inline // global T was hided.
Const T & the_min (const T & v1, const T & v2 ){
If (compare (v1, v2) =-1)
Return v1;
Else
Return v2;
} 2. Restrictions on using template parameter names
The name used as the template parameter cannot be reused within the template.
Just like reusing function parameters, the names of template parameters may be reused in different templates.
3. template declaration
Like any other function or class, a template can be declared but not defined. The declaration must indicate that a function or class is a template.
Template <typename T> inline // global T was hided.
Const T & the_min (const T & v1, const T & v2 );
Template <typename T> inline // global T was hided.
Const T & the_min (const T & v1, const T & v2 );
In the same template declaration and definition, the template parameters do not have to have the same name.
The class or typename keyword must be included before each template type parameter, and the type name must be included before each non-type parameter. It is incorrect to omit the keyword or type specifier.
From xufei96's column