When a template function is defined explicitly within a
compilation unit , this definition is used in preference to any
instantiation from the function template. For example, if one
compilation unit contains the code:
#include <iostream.h> template <class T> T f(T i) {return i+1;} void main() { cout << f(2) << endl; }
and another contains:
int f(int i) {return i+2;}
when compiled and run, the program prints the number 4 to standard output, indicating that the explicitly defined function was used to resolve the call to f().
Each template, whether of a class or of a function, must be defined at most once within a compilation unit. The same applies to an explicitly defined template class or function. Function templates and class templates can be declared many times.
A template class is considered declared if its name is used. A template function is considered declared if any of the following applies:
A template function is instantiated or generated if the function is referenced in any of the following ways, provided that function is not explicitly defined elsewhere in the program:
When a template function is instantiated, the body of the function template is compiled using the template argument list of the template class to instantiate the template arguments. Any errors in the function definition are flagged at this time. If a template function is never generated from a function template, it is not compiled. In this case, some errors in the function definition might not be flagged by the compiler.
Function Templates
Overloading Resolution for Template
Functions
Explicitly Defined Template Functions
Differences between Class and Function
Templates