Explicit Instantiation

Explicit instantiation lets you create an instantiation of a templated class or function without actually using it in your code. Since this is useful when you are creating library (.LIB) files that use templates for distribution, uninstantiated template definitions are not put into object (.OBJ) files.

The following explicitly instantiates MyStack for int variables and six items:

template class MyStack<int, 6>;

This statement creates an instantiation of MyStack without reserving any storage for an object; code is generated for all members.

The following explicitly instantiates only the constructor member function:

template MyStack<int, 6>::MyStack( void );

Visual C++ 6.0 supports explicit instantiation of function templates. Versions prior to 5.0 supported the explicit instantiation of class templates only. For example, the following code is now legal:

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

//Instantiate f with the explicitly specified template
//argument ‘int’
//
template void f<int> (int);

//Instantiate f with the deduced template argument ‘char’
//
template void f(char);

Microsoft Specific

You can use the extern keyword to prevent the automatic instantiation of members. For example:

extern template class MyStack<int, 6>;

Similarly, you can mark specific members as being external and not instantiated as follows:

extern template MyStack<int, 6>::MyStack( void );

Note   The extern keyword in the specialization only applies to member functions defined outside of the body of the class. Functions defined inside the class declaration are considered inline functions and are always instantiated.

END Microsoft Specific