A template function may be overloaded either by template functions or ordinary functions. In such cases, the overloading resolution is accomplished using following steps:

  • Call an ordinary function that has an exact match.
  • If a match for an ordinary function is not found, call a template function that could be created with an exact match.

An error is generated if no match is found. Note that no automatic conversions are applied to arguments on the template functions. The program shows how a template function is overloaded with an explicit function.

Member Function Templates

When we created a class template for vector, in the previous tutorial, all the member functions were defined as inline which was not necessary. We could have defined them outside the class as well. But remember that the member functions of the template classes themselves are parameterized by the type argument (to their template classes) and therefore these functions must be defined by the function templates. It takes the following general form:

Example:

Non-Type Template Arguments

We have seen that a template can have multiple arguments. It is also possible to use non-type arguments. That is, in addition to the type argument T, we can also use other arguments such as strings, functions, constant expressions, and built-in types.

Consider the following example:

This template supplies the size of the array as an argument. This implies that the size of the array is known to the compiler at the compile time itself. The arguments must be specified whenever a template class is created,

Example: