Sunday, Nov 18th

Last update12:59:40 PM GMT

Function Templates

Write e-mail

Function Templates in C++ offer a wide range of use to programmers, even though their full potential is not utilised completely by programmers. Many a times we come across similar functions within the classes which differ only in the datatype. The C++ templates allow us to abstract the algorithm from the datatype.


Lets take an example. We have functions to add two numbers. One function adds to integers and returns an integer and similarly the other adds two floats and returns a float. Assuming we make use of function overloading still we need to create two separate signatures and two definitions for the two functions.

int add(int a,int b) { return (a+b);} // function Without C++ template
float add(float a, float b) { return (a+b);} // function Without C++ template

This is where function templates come to the rescue. Function templates are special kind of functions that operate with generic datatypes. So we are able to create a single template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that is used to pass a type as argument, similar to the way a regular function parameter is used to pass value to a function, template parameter also allow us to pass types to a function. These function templates can use these parameters as if they were any other regular type and the entire operation is reduced to a single C++ template.

The format of C++ function template is:

template <class identifier> function_declaration;
template <typename identifier> function_declaration;

There is no difference between the two formats as such except the use of keyword: class or typename, even though the first one is more commonly used. That is also the syntax we will be using in the examples to come.

So now we come to the point. To create the function template for the above add functions we will do it like this:

template <class myType>
myType add (myType a, myType b) {

Here myType is the function parameter. To use this function template we have the following format for the function call:

function_name <type> (parameters);

So to use the template for adding two integers x and y and then for adding two floats we will have something like below:

int x, y;
int z = add <int> (x,y);
float p,q;
float r = add <float> (p,q);

Here we follow this with another example:

#include <iostream>
using namespace std;
template <class T>
T GetMax (T a , T b)
 T result;
 result = ( a>b) ? a:b;
 return (result);
int main()
 int i=5, j=6,k;
 float l=10, m=5, n;
 k = GetMax <int> (i,j);
 n = GetMax <float> (l,m);
 return 0;

The logic of the above program is self explanatory. Here we have used T instead of myType as the template parameter as it is shorter and more commonly used one. One more thing to take into account here is that in cases like above where generic type T is used as a parameter for template function, the use of  <template parameter> in the template fuction call is not really required. In the above prgram we could very well have written the function calls in the below manner:

k = GetMax (i,j);

n = GetMax (l,m);

Here the compiler could have on itself determined on the basis of the type of function parameter, template corresponding to which datatype is to be instantiated without the need of specifying the data type in angle bracket. 

Share this post

Add comment

Please refrain from using slang or abusive language in the comments.
To avoid waiting for your comment to be published after being reviewed, please log in as a registered user.

Security code

Web Hosting