Explicit Instantiation

Explicit instantiation lets you create an instantiation of a templated class or function without actually using it in your code. Because 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 );

Function templates can be explicitly instantiated by redeclaring them with a specific type argument, as seen in the example in Function Template Instantiation.

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 );

Nota

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.

See Also

Reference

Function Templates