A function template is a function which contains generic code to operate on different types of data. This enables a programmer to write functions without having to specify the exact type of parameters. Syntax for defining a template function is as follows:
1 2 3 4 5 6 7 | template<class Type, ...> return–type function–name(Type arg1, ...) { //Body of function template } |
As shown above, the syntax starts with the keyword template followed by a list of template type arguments or also called generic arguments.
The template keyword tells the compiler that what follows is a template. Here, class is a keyword and Type is the name of generic argument.
Following program demonstrates a template function or function template:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; template < class Type > void summ(Type x, Type y) { cout << "Sum is: " << x + y << endl; } int main() { int a = 10, b = 20; summ(a, b); float p = 1.5, q = 2.4; summ(p, q); return 0; } |
Output for the above program is as follows:
1 2 3 4 | Sum is: 30 Sum is: 3.9 |
In the above program compiler generates two copies of the above function template. One for int type arguments and the other for float type arguments. The function template can be invoked implicitly by writing summ(val1, val2) or explicitly by writing summ<int>(val1, val2).
Guidelines for Using Template Functions
While using template functions, programmer must take care of the following:
Generic Data Types
Every template data type (or generic data type) should be used as types in the function template definition as type of parameters. If not used, it will result in an error. Consider the following example which leads to an error:
1 2 3 4 5 6 7 | template<class Type> void summ(int x, int y) //Error since Type is not used for arguments x and y { cout<<“Sum is: “<<x+y<<endl; } |
Also using some of the template data types is also wrong. Consider the following example which leads to an error:
1 2 3 4 5 6 7 | template<class Type1, class Type2> void summ(Type1 x, int y) //Error since Type2 is not used { cout<<“Sum is: “<<x+y<<endl; } |
Overloading Function Templates
As normal functions can be overloaded, template functions can also be overloaded. They will have the same name but different number of or type of parameters. Consider the following example which demonstrates template function overloading:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> using namespace std; void summ(int x, int y) { cout << "Normal Function: " << endl; cout << "Sum is: " << x + y << endl; } template < class Type1, class Type2 > void summ(Type1 x, Type2 y) { cout << "Template Function: " << endl; cout << "Sum is: " << x + y << endl; } int main() { int a = 10, b = 20; summ(a, b); float p = 1.5, q = 2.4; summ(p, q); return 0; } |
Output for the above program is as follows:
1 2 3 4 5 6 | Normal Function: Sum is: 30 Template Function: Sum is: 3.9 |
Whenever a compiler encounters the call to a overloaded function, first it checks if there is any normal function which matches and invokes it. Otherwise, it checks if there is any template function which matches and invokes it. If no function matches, error will be generated.
From the above example you can see that for the call summ(a, b), normal function is invoked and for the call summ(p, q), template function is invoked.
Take your time to comment on this article.