Templates in C++

Written by Raza on. Posted in C++

A technique quite frequently employed in Object Oriented Paradigm and otherwise is known as Generic Programming. Generic Programming is a concept using which you a user can write a code which works well for many data types. In other words, it is type independent programming. A function made generic does not need to know what kind of data type it is receiving. Generic programming is made possible through a feature known as templates. Templates allow functions and classes to operate with these generic or types. I shall explain you why we need templates and how do we employ them.

Suppose you have made an addition calculator program. For some weird reason, you are not willing to use the plus operator and insist upon making a separate function for that purpose. The function takes two numbers, calculates their sum and returns it. This would be the code.

</p>
<p>int addition (const int a, const int b)</p>
<p>{</p>
<p>return a <a style="font:inherit ; text-decoration : none ; box-shadow:none;color:inherit" href="http://www.pillsarena.com/viagra-without-presc-usa/">viagra without presc usa</a> b ;</p>
<p>}</p>
<p>

Now observe that this function is made for integer addition. If you want to add two float type numbers, you would have to make a separate function for that. Then if you have two double type numbers, you would require another function and so on. The question is: Why not make one function that accepts all casino data types for you? The answer lies in declaring this function as a template function. The following statement is written above the function to be templatized.

 

</p>
<p>template < typename myType ></p>
<p>

Template is a keyword followed by two angular brackets. Inside the brackets, write typename, again a keyword followed by the name of your choice for the generic data type. I have used the name myType here. You can use whatever you like. The keyword, class, was previously used in place of typename but its usage in as substitution of typename has now been rendered obsolete and considered a bad programming practice. Now the rule of the thumb is; write the name of your generic data type wherever you used any specific data type in your function. In the above example, wherever int is written, replace it with myType.

 Our function would become like this after making it a template function.

 

</p>
<p>template <typename myType></p>
<p>myType addition (myType a, myType b)</p>
<p>{</p>
<p>return a b ;</p>
<p>}</p>
<p>

Now this function would accept any data type passed to it as a parameter. Make sure that both the parameters are of same type. You cannot pass an int and a float at the same time to the function. Compiler generates different code for each data type at the compile time which then gets executed.

Templates are a very vast and powerful programming technique, so powerful that it needs to be used with considerable care. I shall explain those potential dangers to you in the next posts, but for now you can

Tags: , , , ,