Templates (C++ only)
A template describes a set of related classes or set of related functions in which a list of parameters in the declaration describe how the members of the set vary. The compiler generates new classes or functions when you supply arguments for these parameters; this process is called template instantiation, and is described in detail in Template instantiation (C++ only). This class or function definition generated from a template and a set of template parameters is called a specialization, as described in Template specialization (C++ only).
The template_parameter_list is a comma-separated list of template parameters, which are described in Template parameters (C++ only).
- a declaration or definition of a function or a class
- a definition of a member function or a member class of a class template
- a definition of a static data member of a class template
- a definition of a static data member of a class nested within a class template
- a definition of a member template of a class or class template
The identifier of a type is defined to be a type_name in the scope of the template declaration. A template declaration can appear as a namespace scope or class scope declaration.
template<class T> class Key
{
T k;
T* kptr;
int length;
public:
Key(T);
// ...
};
Key<int> i;
Key<char*> c;
Key<mytype> m;
The compiler would create three instances
of class Key. The following table shows the definitions
of these three class instances if they were written out in source
form as regular classes, not as templates:class Key<int> i; | class Key<char*> c; | class Key<mytype> m; |
---|---|---|
|
|
|
Note that these three classes have different names. The arguments contained within the angle braces are not just the arguments to the class names, but part of the class names themselves. Key<int> and Key<char*> are class names.