Explicit Instantiation


For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

For the latest documentation on Visual Studio 2017, see Explicit Instantiation on docs.microsoft.com. You can use explicit instantiation to 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.

This code 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 next line explicitly instantiates only the constructor member function:

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

You can explicitly instantiate function templates by using a specific type argument to re-declare them, as shown in the example in Function Template Instantiation.

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:

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

You can use the extern keyword to keep the compiler from generating the same instantiation code in more than one object module. You must instantiate the template function by using the specified explicit template parameters in at least one linked module if the function is called, or you will get a linker error when the program is built.

System_CAPS_ICON_note.jpg 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.

Function Templates