Class templates (C++ only)
The relationship between a class template and an individual class is like the relationship between a class and an individual object. An individual class defines how a group of objects can be constructed, while a class template defines how a group of classes can be generated.
- Class template
- is a template used to generate template classes. You cannot declare an object of a class template.
- Template class
- is an instance of a class template.
- The class template definition is preceded by
where template-parameter-list is a comma-separated list of one or more of the following kinds of template parameters:template< template-parameter-list >- type
- non-type
- template
- Types, variables, constants and objects within the class template can be declared using the
template parameters as well as explicit types (for example,
intorchar).
Template parameter packs can also be a kind of parameter for class templates. For more
information, see Variadic templates (C++11).
template<class L, class T> class Key;This reserves the name as a class template name. All template declarations for a class template must have the same types and number of template arguments. Only one template declaration containing the class definition is allowed.
> at the end of the inner list and the > at the end of the
outer list. Otherwise, there is an ambiguity between the extraction operator
>> and two template list delimiters >. template<class L, class T> class Key;
template<class L> class Vector { /* ... */ };
int main ()
{
class Key <int, Vector<int> > my_key_vector;
// implicitly instantiates template}
Beginning of C++11 only.
When the right angle bracket feature is enabled, the >> token is treated as two consecutive > tokens if both the following conditions are true:
- The
>>token is in a context where one or more left angle brackets are active. A left angle bracket is active when it is not yet matched by a right angle bracket. - The
>>token is not nested within a delimited expression context.
If the first > token is in the context of a
template_parameter_list, it is treated as the ending delimiter for the
template_parameter_list. Otherwise, it is treated as the greater-than operator. The
second > token terminates an enclosing template_id template_id
construct or a different construct, such as the const_cast,
dynamic_cast, reinterpret_cast, or static_cast
operator. For example:
template<typename T> struct list {};
template<typename T>
struct vector
{
operator T() const;
}
int main()
{
// Valid, same as vector<vector<int> > v;
vector<vector<int>> v;
// Valid, treat the >> token as two consecutive > tokens.
// The first > token is treated as the ending delimiter for the
// template_parameter_list, and the second > token is treated as
// the ending delimiter for the static_cast operator.
const vector<int> vi = static_cast<vector<int>>(v);
}
A parenthesized expression is a delimited expression context. To use a bitwise shift operator inside template-argument-list, use parentheses to enclose the operator. For example:
template <int i> class X {};
template <class T> class Y {};
Y<X<(6>>1)>> y; //Valid: 6>>1 uses the right shift operator
End of C++11 only.
template<class T> class Vehicle
{
public:
Vehicle() { /* ... */ } // constructor
~Vehicle() {}; // destructor
T kind[16];
T* drive();
static void roadmap();
// ...
};Vehicle<char> bicycle; // instantiates the templatethe constructor, the constructed object, and the member function drive() can be
accessed with any of the following (assuming the standard header file string.h is
included in the program file):
| constructor |
|
object bicycle |
|
function drive() |
char* n = bicycle.drive(); |
function roadmap() |
Vehicle<char>::roadmap(); |