C++ Function Templates overloading

Function templates in C++ are used if the same set of code/algorithm needs to be applied for different data types. The same logic need not be repeatedly written for different data types, involving lots of manual effort in development, testing, debugging and fixing. Instead C++ compiler itself takes the burden and produces different signatures for each data type, if templates are used. The C++ function templates explains some of the c++ function templates basics.

C++ Function templates can also be overloaded as like normal functions. In normal C++ function overloading, the function can have different data types as well as different number of parameters. But in case of templates, the overloading happens only with the number of parameters, as it can already handle many data types.

Declaring Function Templates – C++ Function Templates Overloading:

Function template declarations usually include a keyword typename or class, to specify that the parameters are going to be of generic type. The following code snippets include declarations for two different overloaded min functions, with the ability to handle 2 and 3 parameters each.

#include <iostream.h>
template <class T>
T&  min(T  &tParam1, T  &tParam2)
{
if(tParam1 < tParam2)
return tParam1;
else
return tParam2;
}

template <class T>
T&  min(T  &tParam1, T  &tParam2, T  &tParam3)
{
if(min(tParam1, tParam2) < tParam3)
return min(tParam1, tParam2) ;
else
return tParam3;
}

Calling Function Templates – C++ Function Templates Overloading:

Now we can call min function with 2 or 3 parameters. The following are some of the ways of calling the function templates declared above.

int l_intValue;
l_intValue = min<>(9,6); //calls min(int,int) 
cout<<"Minimum of int: "<<l_intValue<<endl;

double l_dblValue;
l_dblValue= min(10.5,11.3); //Calls min(double,double)
cout<<"Minimum of Double: "<<l_dblValue<<endl;

double l_dblValue1;
// l_dblValue1= min(10,11.3); //Returns Error saying ambiguous call
// cout<<"Minimum of Double: "<<l_dblValue1<<endl;

l_intValue = min(3,6,4);
cout<<"Minimum int 3 Parameters: "<<l_intValue<<endl;

The above code sample calls the function templates with only 2 different data types. It can handle all data types. For every such data type, the C++ compiler generates a function signature and uses it when the function is called.

A very important point to be noted here is, the function templates will not be able to handle any ambiguous calls. The code sample in the red colored section, cannot be resolved by the C++ compiler. This can be resolved by explicitly specifying the data type inside angular brackets.

l_dblValue1= min<double>(10,11.3); //Can be resolved by C++ compiler

Mixing Normal functions – C++ Function Templates Overloading:

The c++ function templates overloading can also be mixed with normal c++ functions. The following function signature can also be mixed with the above code and this will also be used by the C++ compiler while resolving function calls.

int min(int fParam1,int fParam2)
{

	cout << "Normal function called for "<< fParam1 << " " <<fParam2 <<endl;

	if(fParam1 < fParam2)
	return fParam1;
	else
	return fParam2;
}

If the above normal function is mixed, this can handle the ambiguous function calls also. Another point to be noted is, the resolution mechanism for a function call will always prefer a normal c++ function in case of equals. But if the best match is with the template function, it will prefer the templates. If one wants to instruct the compiler to specifically use a C++ function template, he can do so as in the code section marked in green color.