With the external instances method, all instances are placed within the template repository. The compiler ensures that exactly one consistent template instance exists; instances are neither undefined nor multiply defined. Templates are reinstantiated only when necessary. For non-debug code, the total size of all object files (including any within the template cache) may be smaller with -instances=extern than with -instances=global.
The disadvantage of this method is that the cache must be cleared whenever you change programs or make significant program changes. The cache is a bottleneck for parallel compilation, as when using dmake because access to the cache must be restricted to one compilation at a time. Also, you can build only one program within a directory.
Determining whether a valid template instance is already in the cache can take longer than just creating the instance in the main object file and discarding it later if needed.
Because instances are stored within the template repository, you must use the CC command to link C++ objects that use external instances into programs.
example% CC -xar -instances=extern -o libmain.a a.o b.o c.o
Do not run different compiler versions in the same directory due to possible cache conflicts when you specify -instance=extern. Consider the following when you compile with the -instances=extern template model:
Do not create unrelated binaries in the same directory. Any binaries (.o, .a, .so, executable programs) created in the same directory should be related, in that names of all objects, functions, and types common to two or more object files have identical definitions.
It is safe to run multiple compilations simultaneously in the same directory, such as when using dmake. It is not safe to run any compilations or link steps at the same time as another link step. A link step is any operation that creates a library or executable program. Be sure that dependencies in a makefile do not allow any commands to run in parallel with a link step.
With the static instances method, all instances are placed within the current compilation unit. As a consequence, templates are reinstantiated during each recompilation; instances are not saved to the template repository.
The disadvantage of this method is that it does not follow language semantics and makes substantially larger objects and executables.
Instances receive static linkage. These instances will not be visible or usable outside the current compilation unit. As a result, templates might have identical instantiations in several object files. Because multiple instances produce unnecessarily large programs, static instance linkage is suitable only for small programs where templates are unlikely to be multiply instantiated.
Compilation is potentially faster with static instances, so this method might also be suitable during Fix-and-Continue debugging. (See Debugging a Program With dbx.)
Unlike with early compiler releases, you do not have to guard against multiple copies of a global instance.
The advantage of this method is that incorrect source code commonly accepted by other compilers is now also accepted in this mode. In particular, references to static variables from within a template instances are not legal but are commonly accepted.
The disadvantage of this method is that individual object files may be larger due to copies of template instances in multiple files. If you compile some object files for debug using the -g option and some without, it is hard to predict whether you will get a debug or non-debug version of a template instance linked into the program.
Template instances receive global linkage. These instances are visible and usable outside the current compilation unit.
In the explicit instances method, instances are generated only for templates that are explicitly instantiated. Implicit instantiations are not satisfied. Instances are placed within the current compilation unit.
The advantage of this method is that you have the least amount of template compilation and smallest object sizes.
The disadvantage is that you must perform all instantiation manually.
Template instances receive global linkage. These instances are visible and usable outside the current compilation unit. The linker recognizes and discards duplicates.
When you use the semi-explicit instances method, instances are generated only for templates that are explicitly instantiated or implicitly instantiated within the body of a template. Instances required by explicitly created instances are generated automatically. Implicit instantiations in the mainline code are not satisfied. Instances are placed within the current compilation unit. As a consequence, templates are reinstantiated during each recompilation; instances receive global linkage and they are not saved to the template repository.