On Tue, 26 Jun 2007, Silvius Rus wrote:
> Herman Geza wrote: > > void foo(float *a) { > > int *b = (int*)a; // type-punning warning > > > > // here, access to a and b shouldn't be optimized, as the compiler > > knows that a and b point to the same address > > } > > > > Is this reasonable? > > > Even if it were trivial to implement, I would vote against it, because it > would encourage people to write non-compliant code. Agreed, if it results in non-compilant code. I have problems with 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 use asPoint very trivially, it is very easy to detect (for a human) that references point to the same address. Like void doSomething(Point p) { // do something with p } void fv() { Point a, b; // initialize a & b doSomething((a-b).asPoint()); // a-b results in a Vector } Here, there's a bad compilation (I think) at the call of doSomething. I solved my problem with struct Vector { Point toPoint() { return Point(x, y, z); } }; GCC optimizes this as well, but I use other compilers which don't. It is not a big issue, I just wanted to know some background behind the warning :) Thanks for your responses. Geza