g++ 4.3, troubles with C++ indexing idioms

Dave Korn dave.korn@artimi.com
Thu Jul 19 18:59:00 GMT 2007

On 19 July 2007 14:08, tbp wrote:

> I have that usual heavy duty 3 fp components class that needs to be
> reasonably efficient and takes this form for g++
> struct vec_t {
> 	float x,y,z;
> 	const float &operator()(const uint_t i) const { return *(&x + i); }
> 	float &operator()(const uint_t i) { return *(&x + i); } // <-- guilty
> 	[snip ctors, operators & related cruft]
> };
> I use this notation 

  That's not "notation".  That's "illegal invalid code that invokes undefined
behaviour".  struct vec_t is not a POD type (since it has ctors) and you don't
actually have any right to assume that x, y and z are arranged in consecutive
memory locations.

> Alas, somewhere between gcc-4.3-20070608 (ok) and gcc-4.3-20070707
> (not ok ever since), the non const indexing started to trigger bogus
> codegen with 

  Bogus codegen is the inevitable result of bogus code.  Garbage in, garbage

  BTW, the const indexing is completely undefined too.

> At this point i'd need some guidance from compiler developers 

  No, you need to read a book on how to program in C++.

> What kind of idiom am i supposed to write such thing in to get back
> efficient and correct code?

  This is a question for gcc-help, not for the main gcc list.  "Legal valid"
code would be a good start.

Can't think of a witty .sigline today....

More information about the Gcc mailing list