reinterpret_cast<> and UB

Discussion in 'C++' started by Nate Barney, Sep 7, 2006.

  1. Nate Barney

    Nate Barney Guest

    I have:

    // base class for Vector and Matrix
    template <unsigned N,typename Value=float>
    class NonScalar
    {
    public:

    Value *ptr() { return e; }
    const Value *ptr() const { return e; }

    private:

    Value e[N];
    };

    // Vector class
    template <unsigned N,typename Value=float>
    class Vector : public NonScalar<N,Value>
    {
    public:

    Vector() {}
    Vector(const NonScalar<N,Value> &ns) : NonScalar<N,Value>(ns) {}
    };

    // Matrix class
    template <unsigned R,unsigned C,typename Value=float>
    class Matrix : public NonScalar<R * C,Value>
    {
    public:

    Matrix() {}
    Matrix(const NonScalar<R * C,Value> &ns) : NonScalar<R *
    C,Value>(ns) {}

    // Matrix multiplication operator
    template <unsigned N>
    Matrix<R,N,Value> operator*(const Matrix<C,N,Value> &m) const;
    }

    // Matrix * Vector operator
    template <unsigned R,unsigned C,typename Value>
    inline Vector<R,Value> operator*(const Matrix<R,C,Value> &m,
    const Vector<C,Value> &v)
    {
    return static_cast<const Vector<R,Value>&>(m *
    static_cast<const Matrix<C,1,Value>&>(v));
    }

    // Vector * Matrix operator
    template <unsigned R,unsigned C,typename Value>
    inline Vector<C,Value> operator*(const Vector<R,Value> &v,
    const Matrix<R,C,Value> &m)
    {
    return static_cast<const Vector<C,Value>&>(
    static_cast<const Matrix<1,R,Value>&>(v) * m);
    }

    The v*m and m*v operators work correctly, but the second static_cast<>
    in each one copies the contents of the Vector into a temporary Matrix,
    via the NonScalar copy constructor. What I'd like to do is replace
    this cast with a reinterpret_cast<>, which would simply treat the
    Vector like a Matrix for the purposes of the call. The question is:
    would doing so invoke undefined behavior? If so, is there a way to
    make these operators work without copying their contents?

    Thanks,
    Nate
     
    Nate Barney, Sep 7, 2006
    #1
    1. Advertising

  2. Nate Barney

    Nate Barney Guest

    Nate Barney wrote:
    >
    > // Matrix class
    > template <unsigned R,unsigned C,typename Value=float>
    > class Matrix : public NonScalar<R * C,Value>
    > {
    > /* snip */
    > }


    Oops, forgot the semicolon here.
     
    Nate Barney, Sep 7, 2006
    #2
    1. Advertising

  3. Nate Barney

    Ron Natalie Guest

    Nate Barney wrote:

    >
    > The v*m and m*v operators work correctly, but the second static_cast<>
    > in each one copies the contents of the Vector into a temporary Matrix,
    > via the NonScalar copy constructor. What I'd like to do is replace
    > this cast with a reinterpret_cast<>, which would simply treat the
    > Vector like a Matrix for the purposes of the call. The question is:
    > would doing so invoke undefined behavior? If so, is there a way to
    > make these operators work without copying their contents?
    >


    No. The fact that the two classes are derived from a common base
    and there are no virtual functions in play or data members in the
    derived class indicate it might work, the standard doesn't require
    it.

    What I might do to accomplish this is provide a Matrix class
    (or a Matrix-like) class that is initialized with a Vector
    and gets at the underlying NonScalar by reference rather than
    making a copy.
     
    Ron Natalie, Sep 8, 2006
    #3
  4. Nate Barney

    Nate Barney Guest

    Ron Natalie wrote:
    >
    > No. The fact that the two classes are derived from a common base
    > and there are no virtual functions in play or data members in the
    > derived class indicate it might work, the standard doesn't require
    > it.


    That's what I was afraid of. Oh well. Thanks for clearing that up for
    me.

    > What I might do to accomplish this is provide a Matrix class
    > (or a Matrix-like) class that is initialized with a Vector
    > and gets at the underlying NonScalar by reference rather than
    > making a copy.


    That's not a bad idea. I'm also thinking about pushing operator*
    down to NonScalar. I'll have to do a bit more thinking, though,
    it seems.

    Thanks again,
    Nate
     
    Nate Barney, Sep 8, 2006
    #4
    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. Christopher Benson-Manica

    reinterpret_cast and enumerations

    Christopher Benson-Manica, Nov 2, 2004, in forum: C++
    Replies:
    5
    Views:
    887
    Ron Natalie
    Nov 3, 2004
  2. Kobe
    Replies:
    3
    Views:
    626
    Tomás
    Feb 15, 2006
  3. Alex Vinokur
    Replies:
    4
    Views:
    605
    Jakob Bieling
    Mar 27, 2006
  4. ciccio
    Replies:
    4
    Views:
    1,171
    James Kanze
    Apr 15, 2008
  5. Alex Vinokur
    Replies:
    1
    Views:
    611
Loading...

Share This Page