Function Templates

In this lesson, we will learn about function templates and their uses.

A function template is defined by placing the keyword template in front of the function template followed by type or non-type parameters.

  • The keyword class or typename declares the parameters.
  • The name T is usually used for the first parameter.
  • The parameter can be used in the body of the function.

Let’s take a look at an example of function templates:

Press + to interact
template <typename T>
void xchg(T&x , T&y){
...
template <int N>
int nTimes(int n){
...
...