Hey everyone!
Today I learned a really great special functions that I did not know about before β templates.
Basically, all it does lets you to define other functions with arguments, but those arguments can be of any different type. Thus, we do not have to overload functions just because they have to accept different data types. I really like this because I don’t really like to overload functions too much. It’s might not be easy to understand what I mean without an example so here it is:
for example we have function:
void findLargerNumber (int a, int b)
{
if (a == b)
cout << a << ” = ” << b << endl;
else
{
int i = a > b;
if (i)
cout << a << endl;
else
cout << b << endl;
}
}
if our arguments are doubles (or any other variable type) and we will need to perform same calculations we will need to overload findLargerNumber function with arguments for double… and for float and for long long… That could be a real pain!… It is inefficient and takes a lot of time to do… unless we use templates which will allow us to code function only once.
Here is an example:
template <class AnyType>
void findLargerNumber (const AnyType &a, const AnyType & b)
{
if (a == b)
cout << a << ” = ” << b << endl;
else
{
int i = a > b;
if (i)
cout << a << endl;
else
cout << b << endl;
}
}
all we do is: before our function findLargerNumber we add
template <class AnyType>
which called template prefix
and we change argument types to AnyType which will be out type parameter. And that is all.
Here is the program with a little main:
#include <iostream>
using namespace std;
template <class AnyType>
void findLargerNumber (const AnyType &a, const AnyType & b)
{
if (a == b)
cout << a << ” = ” << b << endl;
else
{
int i = a > b;
if (i)
cout << a << endl;
else
cout << b << endl;
}
}
int main()
{
findLargerNumber(3, 8);
findLargerNumber(5, 2);
findLargerNumber(5, 5);
findLargerNumber(3.5, 8.2);
findLargerNumber(13.0, 8.6);
findLargerNumber(‘A’, ‘a’);
return 0;
}
Isn’t cool? And notice that we do not have to write any extra code in main to call template function. So, in summary, it saves a lot of time. It can also be used with classes, but that one should be explained by Fardad in class some time later π
Dale Karp
Sep 12, 2010 @ 05:24:49
Very interesting post, Marina! Is there a reason the parameters in the example without templates are not references while the example with templates are?
mysnogorodsky
Sep 12, 2010 @ 13:26:44
Hi Dale! Thank you for visiting my blog.
There is no specific reason to pass parameters in templates by reference. I did it to demonstrate that it is possible to pass in by reference or by value. However, we know that to pass parameters by value is not very efficient. If you pass parameters by value in my template example the output will be exactly the same. I hope this answers your question.
Here is main to try:
Notice here I also changed the body of template function a bit.
Before I used technique Fardad demonstrated in class
int i = a > b
which return true or false.
This is how I learn usually, try it for a few times and then I will remember it forever (hopefully) π