question about template

W

wongjoekmeu

Hello all,

I have a question about templates. Let say I have the following
template function
-----
template <class T>
T max(T a, T b)
{
return a > b ? a : b ;
}
-----
As far as what I understand if you make a statement as "max(2,3)", "T"
will be replace by "int" and a function like "int max(int a, int b)"
will be called. If I insert floats, then I will get "float max(float a,
float b)". As least that is what I understand from what I have read.
First of all I was wondering whether I am right or not. Now, let us
assume that I am correct. Let us now say that I have a class called
"Cat" and create two instances of "Cat" like "cat1" and "cat2".
Now what will happen if I make a statement like "max(cat1,cat2)" ????
will this evolves to the calling of a function like "Cat max(Cat cat1,
Cat cat2)" ??? If yes what does Cat1>Cat2 will return ? True ? or false
? To what will it evaluate.
Now suppose I have in the class a public member integer called, "age".
And in fact I meant to compare the age of the two cats yet keeping the
previous declaration of my function template. Can I overload my first
template function ??
Thank you very much in advance for answering my question.

Robert
 
D

David Harmon

On 2 Feb 2005 02:02:58 -0800 in comp.lang.c++, "(e-mail address removed)"
Now what will happen if I make a statement like "max(cat1,cat2)" ????
will this evolves to the calling of a function like "Cat max(Cat cat1,
Cat cat2)" ???
Yes.

If yes what does Cat1>Cat2 will return ? True ? or false

Whatever your operator>() function that you write returns. Yes, it
would probably be a good idea for you to have it to meaningfully
return a bool true or false.
 
I

ishijak

When you are using templates function, for every T used in your code,
the compiler generates coresponding concrete functions. So, if you use
some operations on T, those operations have to be available to the
compiler. The template to work with Cat, you have to overload the
operator> for Cat type.

bool operator>(Cat& c1, Cat& c2)
{
return c1.age > c2.age;
}
 
W

wongjoekmeu

But I am still remain with the question whether I can overload a
template function which will return a meaning full answer. If yes, how
do you overload a template function ??
Robert
 
R

Rolf Magnus

But I am still remain with the question whether I can overload a
template function which will return a meaning full answer.

So you do not want to write an operator> for your class? I think that would
be more logical, since if there is a way to find out which is the "greater"
of two instances of your class, it would make sense to also have a way to
find out whether an object is "greater" than another one.
If yes, how do you overload a template function ??

I think you can overload it, but usually, you don't. Either you specialize
the template, like:

template <class T>
T max(T a, T b)
{
return a > b ? a : b ;
}

template <>
Cat max(Cat a, Cat b)
{
return // whatever you think is appropriate
}

Or you give it an extra parameter for the comparison function:

template <class T, class Cmp = std::less<T> >
T max(T a, T b, Cmp cmp = Cmp())
{
return cmp(b, a) ? a : b ;
}

Now max has an optional third parameter. By default, std::less is used,
which in turn uses operator< for the comparison (The standard library
usually prefers < over >, so only one of the two is needed), but you can
write your own function or functor to do any comparison you want and then
provide it as third argument to max. This way, you are not limited to one
way of comparing two objects of your class.
 
I

ishijak

There are rules for resolution if more then one function or template
function matches you function call.
if you need deatails you need, see:

The C++ Programming Language
Stroustrup
Third Edition
Chapter
13.3.2 Function Template Overloading [temp.over]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top