Explicit Instantiation of Function Templates Sample

Description

Typically a programming environment will automatically take care of instantiating templates that are used in a program. But sometimes it's desirable for performance or other reasons to explicitly take control of instantiation. C++ provides a mechanism for explicitly instantiating a function template. For example, with this code:

	template <class T> void f(T) {...}

	template void f(double);
the compiler or environment is told to explicitly instantiate f(double), with T bound to double as part of the instantiation process.

Template parameters may also be explicitly specified using the <> notation.

Concept

In the example program, two function templates are defined, and then specific instances of them are explicitly instantiated. <double> is specified in the first case with f(), because the function itself takes no parameters and the type of T cannot otherwise be deduced.

In the second case, <char> is specified, because template parameter types cannot be deduced from the return type of a function.

Special Notes:

One instantiation strategy is to group instantiations into a single compilation unit. A related issue is use of the export keyword in C++. These mechanisms interact with whatever environment support is offered for instantiation, and it's hard to prescribe a "right" approach.

Supported
Supported
Supported