### Class Template Declarations and Definitions

A class template must be declared before any declaration of a corresponding template class. A class template definition can only appear once in any single compilation unit. A class template must be defined before any use of a template class that requires the size of the class or refers to members of the class.

In the following example, the class template key is declared before it is defined. The declaration of the pointer keyiptr is valid because the size of the class is not needed. The declaration of keyi, however, causes an error.

    template  <class L> class key;      // class template declared,
                                        // not defined yet
                                        //
    class key<int> *keyiptr;            // declaration of pointer
                                        //
    class key<int> keyi;                // error, cannot declare keyi
                                        // without knowing size
                                        //
    template <class L> class key        // now class template defined
    {
    // ...
    };

If a template class is used before the corresponding class template is defined, the compiler issues an error. A class name with the appearance of a template class name is considered to be a template class. In other words, angle brackets are valid in a class name only if that class is a template class.

The definition of a class template is not compiled until the definition of a template class is required. At that point, the class template definition is compiled using the argument list of the template class to instantiate the template arguments. Any errors in the class definition are flagged at this time. If the definition of a class template is never required, it is not compiled. In this case, some errors in the definition might not be flagged by the compiler. The /Wcls option can be used to find errors in class templates that are not compiled.

A class template can only be defined once within a compilation unit, and the class template name cannot be declared to refer to any other template, class, object, function, value, or type in the same scope.

Class Templates

Nontype Template Arguments
Explicitly Defined Template Classes
Function Templates
Differences between Class and Function Templates