473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

question about template

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

Jul 23 '05 #1
5 1207
On 2 Feb 2005 02:02:58 -0800 in comp.lang.c++, "wo*********@yahoo.com"
<wo*********@yahoo.com> wrote,
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.

Jul 23 '05 #2
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;
}

wo*********@yahoo.com wrote:
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


Jul 23 '05 #3
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

Jul 23 '05 #4
wo*********@yahoo.com wrote:
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.

Jul 23 '05 #5
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]

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott | last post by:
The following is the XML I have to work with. Below is the question <Table0> <CaseID>102114</CaseID> <CaseNumber>1</CaseNumber> <DateOpened>2005-06-14T07:26:00.0000000-05:00</DateOpened>...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
10
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
18
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
16
by: Jeroen | last post by:
Hi all, I have a question which is illustrated by the following piece of code: template <class T> class A { T my_value; }; In a list, I'd like to store pointers to objects of class A....
3
by: DerrickH | last post by:
I have a template class with three policies: PolicyA, PolicyB, and PolicyC. The design dilemma I am having is that B and C depend upon A, but I would like my Host class to derive from all three. In...
9
by: Peskov Dmitry | last post by:
It is a very basic question.Surely i got something wrong in my basic understanding. //Contents of file1.cpp using namespace std; #include <iostream> template <typename T> class my_stack;
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.