TITLE: What is explicit instantiation for? (Newsgroups: comp.std.c++, 27 Jul 98) DOUGLASS: scott douglass > I'm trying to figure out what explicit instantiation if for. OLIVA: Alexandre Oliva There are two situations in which explicit instantiation is useful: 1) for programmers to control in which object file they intend a certain symbol to be defined. By carefully selecting places in which to instantiate templates, it may be possible to reduce the dependencies between modules of a program, causing some modules not to be needlessly included in an executable. 2) in order to allow templates to be defined separately from their declarations, because, if the definition of a template is not provided in every translation unit in which it is used, it must be explicitly instantiated in some translation unit. This is not equivalent to exporting a template definition; exporting has to do with *all* possible specializations of a template, whereas by explicitly instantiating a template for a particular set of types, then hiding its implementation, you can restrict the set of specializations that can be used. DOUGLASS: > Can you give me an example of a program that won't work without > explicit instantiation? OLIVA: The second item above, in which a template is used in several translation units, but is defined in only one of them, is one such case. DOUGLASS: > Is it legal to explicitly instantiate a template in one compilation > unit and implicitly instantiate the same template in another? OLIVA: Yes. DOUGLASS: > Similarly, is it legal to explicitly specialize a template in one > compilation unit and implicitly instantiate the same template in > another? OLIVA: If you really meant `specialize' in the sentence above, then it is illegal, because it violates the ODR, but no diagnostic is required.