Function Template Instantiation
When a function template is first called for each type, the compiler creates an “instantiation,” this is a version of the templated function specialized for the type. This instantiation will be called every time the function is used for the type. If you have several identical instantiations, even in different modules, only one copy of the instantiation will end up in the executable file.
Conversion of function arguments is allowed in function templates for any argument and parameter pair where the parameter does not depend on a template argument. For example:
template<class T> void f(T, int){ };
int i; char c;
f(i, c);
In this example, a conversion of i
to type T
may cause an error. However, the conversion of (char c
) to int
is allowed.
Visual C++ 5.0, and later, now supports explicit instantiation of function templates. Previous versions only supported the explicit instantiation of class templates. 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);