Function Templates

A function template allows you to define a group of functions that are the same except for the types of one or more of their arguments or objects. All type arguments in a function template must be used in the argument list or in the class qualifier for the function name. The type of a template function argument need not be explicitly specified when the template function is called. In this respect, a template function differs from a template class.

Note the distinction between the terms function template and template function:

Function template
is a template used to generate template functions. A function template can be only a declaration, or it can define the function.

Template function
is a function generated by a function template.

Because template functions can be generated in all compilation units that contain function template definitions, you may want to group function template definitions into one or two compilation units.



Class Templates


Example of a Function Template


Overloading Resolution for Template Functions
Explicitly Defined Template Functions
Function Template Declarations and Definitions
Differences between Class and Function Templates