Templates and their Importance in C++

Template is a very powerful yet underused feature of C++. It should be used as much as possible.

Let’s begin with a simple scenario. Let’s write a function to multiply 2 numbers in C++.

Now, when we pass the below inputs to the function, we get some results that are shown below.

We can see that the first output (2) is correct. But the output of x*z is an integer, instead of being a decimal(float). This is because the function multiply did implicit conversion of the arguments a & b from float to int. What can we do to solve this problem? Well,you can overload the function to include the float datatype too.

Simple enough. Now, what if we pass both float parameters to the multiply function? Many of you will say that we can just write a function with both the parameters as float so that it can multiply both integer and float datatypes. But, this raises a problem. There’s not a great difference between int and float in terms of storage and memory. But, what if the arguments are of the datatype long or double?  You will write a function with both the parameters as double. That will lead to wastage of memory and space. What if most of the numbers to be multiplied are integer or decimals and the long/double datatype comes rarely?

At this point, I would like to tell you that there is a better way to do this than function overloading. It’s known as function templates. Templates are nothing but a way to write code that is independent of the datatype that can be used on it. Let’s dive into the code to learn more.

Here, the first line declares the template and the type of all the arguments. After that the multiply function gives TYPE datatype input to the 2 arguments. Basically what we have done here is, we have put a place-holder for the datatype in the function which can be changed according to the datatype of arguments in each function call. Let’s try calling multiply using a variety of datatypes.

The above calls give the following output.

Now, in order to compare the performance, let’s write the above code without using templates.

Let’s run this and see how much time it takes.

Execution Time with function overloading: 0.116 secs

Execution Time with templates:  0.036 secs

Hence template code was more than 3 times faster than the code without templates. Now, you must be wondering what is the reason that templates took less time?

When you write N overloads manually, (say N=3), those N different overloads would be anyway be compiled,linked and packed in binary (the executable). However, with templates, only the required instantiations of function would get into final executable. In simpler terms, if you want to multiply 2 and 3(both int type) then the final executable will have only 1 instantiation of the function i.e. multiply(int,int). But, if you do not use templates ans manually create 3 overloads, then the final exe file will have all the 3 instances of the function.

Beyond execution time, the true benefit is that you need not to do copy-pasting the same code, or to manually maintain the code for different data-types, or to write up a new overload for new data-type that arrives later. You would just provide a template of a function, and rest would be managed by compiler.

Leave a Reply

%d bloggers like this: