|
|
An application may use multiple template classes. These have to be instantiated at some point. If this is done in each source file (actually each object file) where the template class is used, then there will be conflicts in the linker with multiply defined symbols, and wasted space.
Another issue concerns instantiating unused members of a template; this is not allowed, but determining usage requires knowledge of the whole program.
C++ also has the notion of ``specializations'', that is a specific version of a template entity to be used in place of the general version. A specialization cannot be discovered until link time.
One way around these problems is to defer instantiation until link time. This approach also has problems, such as recovering information about where templates and types are defined.
Coding standards and several ``manual'' schemes for doing instantiation are discussed in ``Coding standards for template definitions'' and ``Manual instantiation''. Manual means that the user is responsible for directing instantiation, rather than having it done automatically. We will also discuss ``automatic'' instantiation in ``Automatic instantiation''.
The goal of automatic instantiation is to guide painless instantiation. The programmer should be able to compile source files to object code, then link them and run the resulting program without worry about how the necessary instantiations are done. Automatic instantiation is the default for the C++ Compilation System, and it is recommended for most purposes. However, it does have some consequences; we will discuss some practical tips on compile-time performance and a discussion of some of the things that can go wrong.