Back: Compiler Quirks
Forward: Name Mangling
 
FastBack: How GNU Autotools Can Help
Up: Compiler Quirks
FastForward: How GNU Autotools Can Help
Top: Autoconf, Automake, and Libtool
Contents: Table of Contents
Index: Index
About: About this document

16.3.1 Template Instantiation

The problem with template instantiation exists because of a number of complex constraints:

  • The compiler should only generate an instance of a template once, to speed the compilation process.
  • The linker needs to be smart about where to locate the object code for instantiations produced by the compiler.

This problem is exacerbated by separate compilation--that is, the method bodies for List<T> may be located in a header file or in a separate compilation unit. These files may even be in a different directory than the current directory!

Life is easy for the compiler when the template definition appears in the same compilation unit as the site of the instantiation--everything that is needed is known:

 
template <class T> class List
{
private:
  T* head;
  T* current;
};

List<int> li;

This becomes significantly more difficult when the site of a template instantiation and the template definition is split between two different compilation units. In Linkers and Loaders, Levine describes in detail how the compiler driver deals with this by iteratively attempting to link a final executable and noting, from `undefined symbol' errors produced by the linker, which template instantiations must be performed to successfully link the program.

In large projects where templates may be instantiated in multiple locations, the compiler may generate instantiations multiple times for the same type. Not only does this slow down compilation, but it can result in some difficult problems for linkers which refuse to link object files containing duplicate symbols. Suppose there is the following directory layout:

 
src
|
`--- core
|    `--- core.cxx
`--- modules
|    `--- http.cxx
`--- lib
     `--- stack.h

If the compiler generates `core.o' in the `core' directory and `libhttp.a' in the `http' directory, the final link may fail because `libhttp.a' and the final executable may contain duplicate symbols--those symbols generated as a result of both `http.cxx' and `core.cxx' instantiating, say, a Stack<int>. Linkers, such as that provided with AIX will allow duplicate symbols during a link, but many will not.

Some compilers have solved this problem by maintaining a template repository of template instantiations. Usually, the entire template definition is expanded with the specified type parameters and compiled into the repository, leaving the linker to collect the required object files at link time.

The main concerns about non-portability with repositories center around getting your compiler to do the right thing about maintaining a single repository across your entire project. This often requires a vendor-specific command line option to the compiler, which can detract from portability. It is conceivable that Libtool could come to the rescue here in the future.


This document was generated by Gary V. Vaughan on February, 8 2006 using texi2html