Instantiating C++ templates

Coding standards for template definitions

In order for instantiation to work, the C++ compiler must have available both the declaration and definition of each template and template argument type that is used. Normally a template declaration is placed in a .h file, and the definition in a .C file in the same directory. For function templates, the .h file would contain a forward declaration:

   template <class T> void f(T);

An application that uses templates would include the .h file, and the .C file would automatically be included by the compiler. (The file with the definition can have any valid C++ source file suffix, not only .C.) This process is known as ``implicit inclusion'', and is goverened by the CC -T options implicit and no_implicit. Implicit inclusion is on by default. It may be used with either automatic or manual instantiation. It will not work for preprocessed (.i) source files.

The files containing template definitions should containg only template definitions, since they are not compiled in the normal way.

If implicit inclusion is not used, for example, if the declaration and definition files have different basenames or are in different directories, it is necessary to include both files in the application. The rule is simply that to instantiate, the template declaration and definition must both be present or else there must be known rules for finding the template definition.

Next topic: Manual instantiation
Previous topic: The instantiation problem

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005