ILE C/C++ Language Reference

Class Template Declarations and Definitions

C++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 translation 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.

Related References


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]