On Tue, 26 Jun 2007 [EMAIL PROTECTED] wrote: > On Tue, Jun 26, 2007 at 11:42:27PM +0200, Herman Geza wrote: > > 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 use asPoint very trivially, it is very easy to detect (for a human) > > that references point to the same address. Like > > As a "human", I don't see how they are the same. Other than having three > fields with the same type, and same name, what makes them the same? Having the same layout makes them "the same".
> What happens when Point or Vector have a fourth field added? What if somebody > decides to re-order the fields to "float z, x, y;"? Would you expect the > optimization to be silently disabled? Or would you expect it to guess based > on the variables names? Point and Vector "designed" to have the same layout and nobody is allowed to change this. It worked until now :) Now, it seems a bad design, as the standard says at 3.10.15 (incomplete summary by me): a stored value can be accessed only with an lvalue of the dynamic type of the stored object. They justify this with aliasing. So a Vector shouldn't be accessed via a Point. Undefined behavior happens if someone access an object via a different type (unless it's char or unsigned char, which are allowed); same layout doesn't matter, as Andrew P. said. The compiler is allowed to generate bad code in this case, I think. Geza