473,396 Members | 2,039 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,396 software developers and data experts.

reinterpret_cast<> and UB

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,Valueoperator*(const Matrix<C,N,Value&m) const;
}

// Matrix * Vector operator
template <unsigned R,unsigned C,typename Value>
inline Vector<R,Valueoperator*(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,Valueoperator*(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

Sep 7 '06 #1
3 2325
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.

Sep 7 '06 #2
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.
Sep 8 '06 #3
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

Sep 8 '06 #4

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

Similar topics

11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
15
by: Aman | last post by:
Hi, wrote this piece of code on SunOS 5.9 , compiler g++ 2.95.3 trying to see the byte order of an int or short int by converting to char* . doesn't work . the char* cpt doesn't seem to be...
27
by: Noah Roberts | last post by:
What steps do people take to make sure that when dealing with C API callback functions that you do the appropriate reinterpret_cast<>? For instance, today I ran into a situation in which the wrong...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.