Enter the following program and compile with -Wall -fstrict-aliasing int main(int argc,char **argv) { float f = 0.5;
return reinterpret_cast<int &>(f); } g++ will return the following warning message: warning: dereferencing type-punned pointer will break strict-aliasing rules However, this warning message is confusing at best since no pointer handling is involved here. And then, this program cannot exhibit pointer-aliasing breakage since the variable "f" and the return code cannot interpret the same variable as two different types - the re-interpretation of the floating point value as integer-bit pattern is copied before any aliasing problems could possibly enter the scene. Interestingly, g++ explicitly allows the re-interpretation of bit patterns of a specific type thru the "union trick", i.e. union { float i; int o; } u; then assigning the input value to u.i and reading the bit-pattern back from u.o. However, the former "reinterpret_cast" is only implementation-defined (as the implementation defines the bit-pattern transition from float and its re-inter- pretation as int) whereas the latter "union cast" is undefined behaivour: A compiler is free to re-organize the access patterns by moving the assignment to u.i *behind* the read-back from u.o. Thus, the first method is clearly preferable when this kind of type-reinterpretation is apropriate at all, and thus should be allowed to pass without a warning. The latter is only defined because g++ took the freedom to allow it and to avoid the instruction re-ordering. -- Summary: unsuitable aliasing rule warning Product: gcc Version: 4.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: thor at math dot tu-berlin dot de GCC build triplet: i486-pc-linux-gnu GCC host triplet: i486-pc-linux-gnu GCC target triplet: i486-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27464