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.

Note the distinction between the terms class template and template class:
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.
A template definition is identical to any valid class definition that the template might generate, except for the following:
  • The class template definition is preceded by
      template< template-parameter-list >
    where template-parameter-list is a comma-separated list of one or more of the following kinds of template parameters:
    • 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, int or char).

C++0x
Template parameter packs can also be a kind of parameter for class templates. For more information, see Variadic templates (C++0x).
C++0x

A class template can be declared without being defined by using an elaborated type specifier. For example:
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.

C++0x
By using template parameter packs, template declarations for a class template can have fewer or more arguments than the number of parameters specified in the class template.
C++0x

Note: When you have nested template argument lists, you must have a separating space between the > 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
}
Objects and function members of individual template classes can be accessed by any of the techniques used to access ordinary class member objects and functions. Given a class template:
template<class T> class Vehicle
{
public:
    Vehicle() { /* ... */ }    // constructor
    ~Vehicle() {};             // destructor
    T kind[16];
    T* drive();
    static void roadmap();
    // ...
};
and the declaration:
Vehicle<char> bicycle; // instantiates the template

the 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
Vehicle<char> bicycle;

// constructor called automatically,
// object bicycle created
object bicycle
strcpy (bicycle.kind, "10 speed");
bicycle.kind[0] = '2';
function drive() char* n = bicycle.drive();
function roadmap() Vehicle<char>::roadmap();