This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Type-punning


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?

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?

Cheers,
mark

-- 
mark@mielke.cc / markm@ncf.ca / markm@nortel.com     __________________________
.  .  _  ._  . .   .__    .  . ._. .__ .   . . .__  | Neighbourhood Coder
|\/| |_| |_| |/    |_     |\/|  |  |_  |   |/  |_   | 
|  | | | | \ | \   |__ .  |  | .|. |__ |__ | \ |__  | Ottawa, Ontario, Canada

  One ring to rule them all, one ring to find them, one ring to bring them all
                       and in the darkness bind them...

                           http://mark.mielke.cc/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]