Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > covariant template problem

Reply
Thread Tools

covariant template problem

 
 
weinma81@gmail.com
Guest
Posts: n/a
 
      02-28-2008
Hi!

I'm writing a vector-class for some calculations and it look's like i
am having a covariant problem, where i shouldn't:

The Vector-Class Look's like this and works:

template <int _size,class T>
class Vector {
protected:
T vector[_size];
public:
...
virtual Vector<_size,T> operator-(const Vector<_size,T> &v)
const; // that's one of the methods causing problems

As the crossproduct is only defined for a 3D Vector i wanted to write
and inherit a Vector3 from Vector.
The operators should also return the Vector3 so i overwrote them.. and
there comes my covariant problem.
Looks like this:

template <class T>
class Vector3 : public Vector<3,T> {
public:
...
Vector3<T> operator-(const Vector<3,T> &v) const; // <-- Theres my
problem

Compiler cries:
.../Vector3.h:56: error: invalid covariant return type for 'Vector3<T>
Vector3<T>:perator-(const Vector<3, T>&) const [with T = float]'
.../Vector.h:72: error: overriding 'Vector<_size, T> Vector<_size,
T>:perator-(const Vector<_size, T>&) const [with int _size = 3, T =
float]'

don't know what to do.. asked some people but they also said that this
should work..

regards
Weinma
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      02-28-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Hi!
>
> I'm writing a vector-class for some calculations and it look's like i
> am having a covariant problem, where i shouldn't:
>
> The Vector-Class Look's like this and works:
>
> template <int _size,class T>
> class Vector {
> protected:
> T vector[_size];
> public:
> ...
> virtual Vector<_size,T> operator-(const Vector<_size,T> &v)
> const; // that's one of the methods causing problems


Why is that method virtual? Virtual binary operators do not really work all
that well.


> As the crossproduct is only defined for a 3D Vector i wanted to write
> and inherit a Vector3 from Vector.


Don't. Just have a free standing function:

template < typename T >
Vector<3,T>
cross_product ( Vector<3,T> const & lhs, Vector<3,T> const & rhs ) {
...
}

Note that with the inheritance approach, you create two types: Vector3<T>
and Vector<3,T>. These types will not interact all that nicely.


> The operators should also return the Vector3 so i overwrote them.. and
> there comes my covariant problem.
> Looks like this:
>
> template <class T>
> class Vector3 : public Vector<3,T> {
> public:
> ...
> Vector3<T> operator-(const Vector<3,T> &v) const; // <-- Theres my
> problem
>
> Compiler cries:
> ../Vector3.h:56: error: invalid covariant return type for 'Vector3<T>
> Vector3<T>:perator-(const Vector<3, T>&) const [with T = float]'
> ../Vector.h:72: error: overriding 'Vector<_size, T> Vector<_size,
> T>:perator-(const Vector<_size, T>&) const [with int _size = 3, T =
> float]'
>
> don't know what to do.. asked some people but they also said that this
> should work..


Covariant return types are restricted to pointers and references to classes.
See [10.3/5] for details.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      02-28-2008
(E-Mail Removed) a écrit :
> Hi!
>
> I'm writing a vector-class for some calculations and it look's like i
> am having a covariant problem, where i shouldn't:
>
> The Vector-Class Look's like this and works:
>
> template <int _size,class T>
> class Vector {
> protected:
> T vector[_size];
> public:
> ...
> virtual Vector<_size,T> operator-(const Vector<_size,T> &v)
> const; // that's one of the methods causing problems
>
> As the crossproduct is only defined for a 3D Vector i wanted to write
> and inherit a Vector3 from Vector.
> The operators should also return the Vector3 so i overwrote them.. and
> there comes my covariant problem.
> Looks like this:
>
> template <class T>
> class Vector3 : public Vector<3,T> {
> public:
> ...
> Vector3<T> operator-(const Vector<3,T> &v) const; // <-- Theres my
> problem
>
> Compiler cries:
> ../Vector3.h:56: error: invalid covariant return type for 'Vector3<T>
> Vector3<T>:perator-(const Vector<3, T>&) const [with T = float]'
> ../Vector.h:72: error: overriding 'Vector<_size, T> Vector<_size,
> T>:perator-(const Vector<_size, T>&) const [with int _size = 3, T =
> float]'
>
> don't know what to do.. asked some people but they also said that this
> should work..


Covariance works only with pointers and references.

Here you return by value.

The idiom here would be to rather define:
template <int _size,class T>
virtual Vector<_size,T>& Vector<_size,T>:perator-=(const
Vector<_size,T> &v)
{
//substract
return *this;
}

and defines the operations

template <int _size,class T>
Vector<_size,T> operator-(const Vector<_size,T> &lhs,
const Vector<_size,T> &rhs)
{
Vector<_size,T> ret(lhs);
ret-=rhs;
return ret;
}

Then you only need
template <class T>
virtual Vector3<T>& Vector3<T>:perator-=(const Vector<3,T> &v)
{
//special 3 dim op
return *this;
}



Michael
 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      02-28-2008
Michael DOUBEZ a écrit :
> (E-Mail Removed) a écrit :
>> Hi!
>>
>> I'm writing a vector-class for some calculations and it look's like i
>> am having a covariant problem, where i shouldn't:
>>
>> The Vector-Class Look's like this and works:
>>
>> template <int _size,class T>
>> class Vector {
>> protected:
>> T vector[_size];
>> public:
>> ...
>> virtual Vector<_size,T> operator-(const Vector<_size,T> &v)
>> const; // that's one of the methods causing problems
>>
>> As the crossproduct is only defined for a 3D Vector i wanted to write
>> and inherit a Vector3 from Vector.
>> The operators should also return the Vector3 so i overwrote them.. and
>> there comes my covariant problem.
>> Looks like this:
>>
>> template <class T>
>> class Vector3 : public Vector<3,T> {
>> public:
>> ...
>> Vector3<T> operator-(const Vector<3,T> &v) const; // <-- Theres my
>> problem
>>
>> Compiler cries:
>> ../Vector3.h:56: error: invalid covariant return type for 'Vector3<T>
>> Vector3<T>:perator-(const Vector<3, T>&) const [with T = float]'
>> ../Vector.h:72: error: overriding 'Vector<_size, T> Vector<_size,
>> T>:perator-(const Vector<_size, T>&) const [with int _size = 3, T =
>> float]'
>>
>> don't know what to do.. asked some people but they also said that this
>> should work..

>
> Covariance works only with pointers and references.
>
> Here you return by value.
>
> The idiom here would be to rather define:
> template <int _size,class T>
> Vector<_size,T>& Vector<_size,T>:perator-=(const
> Vector<_size,T> &v)
> {
> //substract
> return *this;
> }
>
> and defines the operations
>
> template <int _size,class T>
> Vector<_size,T> operator-(const Vector<_size,T> &lhs,
> const Vector<_size,T> &rhs)
> {
> Vector<_size,T> ret(lhs);
> ret-=rhs;
> return ret;
> }
>
> Then you only need
> template <class T>
> Vector3<T>& Vector3<T>:perator-=(const Vector<3,T> &v)
> {
> //special 3 dim op
> return *this;
> }


and of course

template <class T>
Vector3<T> operator-(const Vector<3,T> &lhs,
const Vector<3,T> &rhs)
{
Vector3<T> ret(lhs);
lhs-=rhs;
return ret;
}

Michael
 
Reply With Quote
 
weinma81@gmail.com
Guest
Posts: n/a
 
      02-28-2008
Thank you for your quick replies..
you have been very helpfull
 
Reply With Quote
 
weinma81@gmail.com
Guest
Posts: n/a
 
      02-28-2008
Thank you for the help..
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rules for covariant returns??? Joost Kraaijeveld Java 3 11-29-2005 05:39 PM
Generics? Covariant return types? Webb Roberts Java 3 11-10-2005 07:21 AM
Problem with covariant return types Rob. C++ 9 06-26-2004 04:29 PM
Covariant Return Types in 1.5 - doesn't work with Generics? Robert Elliot Java 10 06-06-2004 09:47 PM
Covariant parameters Marcin Vorbrodt C++ 6 09-11-2003 01:18 PM



Advertisments