On Tue, 26 Jun 2007, Silvius Rus wrote: > Herman Geza wrote: > > aliasing when GCC (I think) incorrectly treats types different, but they're > > the same. For example, I have: > > > > struct Point { > > float x, y, z; > > }; > > > > struct Vector { > > float x, y, z; > > > > Point &asPoint() { > > return reinterpret_cast<Point&>(*this); > > } > > }; > > > > Point and Vector have the same layout, but GCC treats them different when it > > does aliasing analysis. I have problems when I use Vector::asPoint. > I also think this case should not be flagged. I have seen similar usage in > network programming. Did it actually result in bad code or was it just the > warning that bothered you? Bad code. I thought this warning almost always means bad compiled code if the casted object isn't casted back to its original type. However, I've created a simple example, which does almost the same as the badly compiled sourcecode. But this simple example compiled fine. I'm sure that the badly compiled code is fine (I mean there is no programmer error, except the type-punned warning), because: - valgrind spotted only this (and the code doesn't behave as it should) - if I change asPoint to toPoint (which returns a new Point object, not a reinterpret_cast'd reference to *this), valgrind doesn't complain, and the code runs fine.
I'll try to make a simple example on which GCC produces bad code. Geza