Yeah, I'm aware it may be an stupid question, but I really don't know what a "template" in C++ is, because my knowledge on it is very limited, I don't have any good books on it so I really can't get as much knowledge as I would like from it.

So if anyone can explain to me what Templates, in down-to-earth terms, I will really appreciate it :).

Recommended Answers

All 6 Replies

Okay, You know what is function overloading in C++, do you?
Lets say you create a function named int greater(int a, int b) which returns the greater of the two integer of either a and b.
You define it as

int greater(int a, int b)
{ if( a> b) return a;
  return b;
}

Now you feel that you also need a function which can check which of the two floats are greater. So you again write another greater() function, this time for float:

float greater(float a, float b)
{ if( a> b) return a;
  return b;
}

Note that this function is exactly similar to the previous one except that it works on float.
Also note that the compiler will automatically call the appropriate function, no matter if you passed two ints as the parameter or two floats. Compiler knows which one to call.
The story doesn't ends here, now you decide that you should perhaps write a similar function for doubles:

double greater(double a, double b)
{ if( a> b) return a;
  return b;
}

So now, you had to write 3 different functions each time for different data types.
The things get worst when you use custom classes since now, you have to write a function greater for them too.

The solution are templates. In this technique (sorry for using such a term, it is not really a technique) you tell the compiler itself to write functions for you of any type of data.
What you do is this:

template<typename T>
T greater(T a, T b)
{
   if( a> b) return a;
      return b;
}

This will tell the compiler, to auto-magically generate a function for the appropriate type by it self.
So when a compiler sees the call like this:

int ia=2;
int ib=3;
int iM=greater(ia,ib);

the compiler creates a function ( the first one we discussed) by himself according to template you provided ( perhaps by substituting the letter T in the template by int ) and hence call the function.
If instead you call the function by passing float arguments, the compiler would have generated a float greater(float, float).

So, hence, this way you can save your fingers. Actually, this will be more beneficial than just to save typing: you will have a template for one logic and can apply it to any type of data which we can compare ( in technical terms, which have the operator> defined )


Edit: Cute kid in your avatar :)

commented: Always superb explanations :) +9

Thanks a lot for the links. ArkM, and thanks a lot for the down-to-earth explanation, siddhant3s :).

So it is basically a way of saving myself for making overloaded functions, right? Or do they have another use? And I am assuming you can use templates in OOP too?

EDIT: And in your said function, would I be able to call it passing it two parameters of different variable type?

PS: Haha, thanks for the comment in my avatar. She is an actress.

Thanks a lot for the links. ArkM, and thanks a lot for the down-to-earth explanation, siddhant3s :).

So it is basically a way of saving myself for making overloaded functions, right? Or do they have another use? And I am assuming you can use templates in OOP too?

EDIT: And in your said function, would I be able to call it passing it two parameters of different variable type?

PS: Haha, thanks for the comment in my avatar. She is an actress.

Nope, while they are templated they are both of template type T meaning they have to be the same type. You could, however, do

template <typename T, typename Q>
T some_func(T arg1, Q arg2)
{
  return arg1 > arg2;
}

Though, obviously, the appropriate operator overloading would had to have taken place for it to work.

Not to take over this guy's thread, but a question about syntax of Templates, is there any real difference between <class T> and <typename T> ?

Not to take over this guy's thread, but a question about syntax of Templates, is there any real difference between <class T> and <typename T> ?

nope, no difference. class was in the original specification by Bjarne, typename was later added to avoid confusion. (I personally use typename)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.