C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What does this templated function return?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Tim Conkling
Guest





PostPosted: Thu Sep 22, 2005 9:07 am    Post subject: What does this templated function return? Reply with quote



I have a class that looks like this:

template <class T>
class MyPoint
{
public:

T x;
T y;

MyPoint(T _x, T _y) : x(_x), y(_y) {}
MyPoint operator+(const MyPoint& a) { return MyPoint(x + a.x, y + a.y);
}

};

The following substitutions for the above operator+() function all
compile:

MyPoint<T> operator+(const MyPoint& a) { return MyPoint(x + a.x, y +
a.y); }
MyPoint operator+(const MyPoint& a) { return MyPoint<T>(x + a.x, y +
a.y); }
MyPoint<T> operator+(const MyPoint& a) { return MyPoint<T>(x + a.x, y +
a.y); }

But I'm confused about the differences between them. I'm also confused
about what the type of the returned value of the original function is:

MyPoint operator+(const MyPoint& a) { return MyPoint(x + a.x, y + a.y);
}

Is it a MyPoint<T> or a MyPoint<some value besides T>?

Thanks,
Tim


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Thu Sep 22, 2005 2:21 pm    Post subject: Re: What does this templated function return? Reply with quote



Tim Conkling wrote:
Quote:
I have a class that looks like this:

template <class T
class MyPoint
{
public:

T x;
T y;

MyPoint(T _x, T _y) : x(_x), y(_y) {}
MyPoint operator+(const MyPoint& a) { return MyPoint(x + a.x, y + a.y);
}

};

The following substitutions for the above operator+() function all
compile:

MyPoint a.y); }
MyPoint operator+(const MyPoint& a) { return MyPoint<T>(x + a.x, y +
a.y); }
MyPoint<T> operator+(const MyPoint& a) { return MyPoint<T>(x + a.x, y +
a.y); }

But I'm confused about the differences between them.

Really? There is none.

Quote:
I'm also confused
about what the type of the returned value of the original function is:

MyPoint operator+(const MyPoint& a) { return MyPoint(x + a.x, y + a.y);
}

Is it a MyPoint<T> or a MyPoint<some value besides T>?

Inside the definition of 'MyPoint' template, 'T' is the template argument
passed to it at instantiation point. Also, 'MyPoint' inside 'MyPoint's
definition is the same as 'MyPoint<T>'. The addendum "<T>" is simply
superfluous.

So, my question to you would be *why* would <T> be "some other value" (it
is actually a _type_, not a _value_) than '<T>'?

Also, change 'T' to something else at the top of the definition. Make it
'ValueType' or something. Do you still have the desire to use 'T' in the
operator+? Why? Do you want to make your operator+ a member template?
You could, you know:

template<class U> MyPoint operator +(const MyPoint<U>& a) const
{
return MyPoint(x + a.x, y + a.y);
}

Here, the "straight" 'MyPoint' means the same class as in which it is
defined, whereas 'MyPoint<U>' means another instantiation of 'MyPoint'.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Kodt
Guest





PostPosted: Thu Sep 22, 2005 2:27 pm    Post subject: Re: What does this templated function return? Reply with quote



Remark:
usually, operator+ should be of const argument:
MyPoint operator+(const MyPoint& rhs) const { ...... }

Within a scope of the class template, "MyPoint" means the type of
actual instantiation, and MyPoint<Anything> means some other type (or
same, if Anything is T). You can use both and even mix them.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tim Conkling
Guest





PostPosted: Thu Sep 22, 2005 7:24 pm    Post subject: Re: What does this templated function return? Reply with quote

Quote:
Also, change 'T' to something else at the top of the definition. Make it
'ValueType' or something. Do you still have the desire to use 'T' in the
operator+? Why? Do you want to make your operator+ a member template?
You could, you know:

template<class U> MyPoint operator +(const MyPoint<U>& a) const
{
return MyPoint(x + a.x, y + a.y);
}

Here, the "straight" 'MyPoint' means the same class as in which it is
defined, whereas 'MyPoint<U>' means another instantiation of 'MyPoint'.

V

Ah. This is what I wanted to do -- to be able to add two MyPoints
together regardless of their type (as long as the + operator is defined
for the two types).

Regarding my original message -- is there a "preferred" syntax for
operator+'s parameter and return types? That is, is one of these two
declarations preferred over the other, or is it just a matter of style?

MyPoint operator+ (const MyPoint& a) const {...}
MyPoint<T> operator+ (const MyPoint<T>& a) const {...}

Thanks,
Tim


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Victor Bazarov
Guest





PostPosted: Fri Sep 23, 2005 2:33 pm    Post subject: Re: What does this templated function return? Reply with quote

Tim Conkling wrote:
Quote:
Also, change 'T' to something else at the top of the definition. Make it
'ValueType' or something. Do you still have the desire to use 'T' in the
operator+? Why? Do you want to make your operator+ a member template?
You could, you know:

template<class U> MyPoint operator +(const MyPoint<U>& a) const
{
return MyPoint(x + a.x, y + a.y);
}

Here, the "straight" 'MyPoint' means the same class as in which it is
defined, whereas 'MyPoint<U>' means another instantiation of 'MyPoint'.

V


Ah. This is what I wanted to do -- to be able to add two MyPoints
together regardless of their type (as long as the + operator is defined
for the two types).

Regarding my original message -- is there a "preferred" syntax for
operator+'s parameter and return types? That is, is one of these two
declarations preferred over the other, or is it just a matter of style?

MyPoint operator+ (const MyPoint& a) const {...}
MyPoint<T> operator+ (const MyPoint<T>& a) const {...}

Inside a template declaration like this:

template<typename T> class Blah {
...
};

I *personally* never use "Blah<T>" simply because the "<T>" is unnecessary
and because if I change the T at the top to something else, I won't screw
up the rest of it. Besides, if I ever remove the 'template<typename T>'
from the class (to make it concrete class), I won't have to clean up every
"<T>" in the class definition.

Of course, it's not an error to have it there (in most cases, I can't
think of any that would be, right now). So, it becomes a matter of style
because of it, I would guess.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Frank Chang
Guest





PostPosted: Sat Sep 24, 2005 8:24 pm    Post subject: Re: What does this templated function return? Reply with quote

Victor: In VC7.1 , I could only create an explicit specialization of
your member template

template<class U> MyPoint operator +(const MyPoint<U>& a) const
{
return MyPoint(x + a.x, y + a.y);
}

in the following manner:

template<>
MyPoint<int> MyPoint<int>::operator +(const MyPoint<int>& a)
{
return MyPoint(x + a.x, y + a.y);
}

Is there an alternate way to write this external specialization? Thank
you.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Sun Sep 25, 2005 2:02 pm    Post subject: Re: What does this templated function return? Reply with quote

Frank Chang wrote:
Quote:
Victor: In VC7.1 , I could only create an explicit specialization of
your member template

template<class U> MyPoint operator +(const MyPoint<U>& a) const
{
return MyPoint(x + a.x, y + a.y);
}

in the following manner:

template
MyPoint<int> MyPoint<int>::operator +(const MyPoint<int>& a)
{
return MyPoint(x + a.x, y + a.y);
}

Is there an alternate way to write this external specialization? Thank
you.

IIRC (since you don't quote enough), it's a member template of a class
template, right? So, what are you trying to explicitly specialise and
to what?

template<class T> struct Foo {
T t;
Foo(T t) : t(t) {}
template<class U> Foo operator+(Foo<U> const &f) const
{
return Foo(t + f.t);
};
};

You're not allowed specialise a member template without specialising
the class template first. IOW you need to pick the 'T' and then you
can play with the 'U'. Of course, if you want them to be the same and
'int', then your solution is fine

template<> Foo<int> Foo<int>::operator+(Foo<int> const &f) const
{
return Foo<int>(t + 42);
};

In this case, 'T' is specialised explicitly as 'int' and 'U' is deduced
from the argument.

V



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.