covariant template problem

Discussion in 'C++' started by weinma81@gmail.com, Feb 28, 2008.

  1. Guest

    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>::eek:perator-(const Vector<3, T>&) const [with T = float]'
    .../Vector.h:72: error: overriding 'Vector<_size, T> Vector<_size,
    T>::eek: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
     
    , Feb 28, 2008
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    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>::eek:perator-(const Vector<3, T>&) const [with T = float]'
    > ../Vector.h:72: error: overriding 'Vector<_size, T> Vector<_size,
    > T>::eek: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
     
    Kai-Uwe Bux, Feb 28, 2008
    #2
    1. Advertising

  3. 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>::eek:perator-(const Vector<3, T>&) const [with T = float]'
    > ../Vector.h:72: error: overriding 'Vector<_size, T> Vector<_size,
    > T>::eek: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>::eek: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>::eek:perator-=(const Vector<3,T> &v)
    {
    //special 3 dim op
    return *this;
    }



    Michael
     
    Michael DOUBEZ, Feb 28, 2008
    #3
  4. Michael DOUBEZ a écrit :
    > 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>::eek:perator-(const Vector<3, T>&) const [with T = float]'
    >> ../Vector.h:72: error: overriding 'Vector<_size, T> Vector<_size,
    >> T>::eek: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>::eek: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>::eek: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
     
    Michael DOUBEZ, Feb 28, 2008
    #4
  5. Guest

    Thank you for your quick replies..
    you have been very helpfull
     
    , Feb 28, 2008
    #5
  6. Guest

    Thank you for the help..
     
    , Feb 28, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Elliot
    Replies:
    10
    Views:
    1,170
    Robert Elliot
    Jun 6, 2004
  2. Webb  Roberts

    Generics? Covariant return types?

    Webb Roberts, Nov 10, 2005, in forum: Java
    Replies:
    3
    Views:
    415
    Thomas Hawtin
    Nov 10, 2005
  3. Joost Kraaijeveld

    Rules for covariant returns???

    Joost Kraaijeveld, Nov 29, 2005, in forum: Java
    Replies:
    3
    Views:
    351
    Roedy Green
    Nov 29, 2005
  4. Marcin Vorbrodt

    Covariant parameters

    Marcin Vorbrodt, Sep 10, 2003, in forum: C++
    Replies:
    6
    Views:
    824
    Alf P. Steinbach
    Sep 11, 2003
  5. Rob.
    Replies:
    9
    Views:
    406
    Alf P. Steinbach
    Jun 26, 2004
Loading...

Share This Page