---code--- #include <iostream> #include <iomanip> using namespace std;
int main() { float f(1.0); // cout << "Value: "; // works with any cout before eval!! unsigned int i = *reinterpret_cast<unsigned int*>(&f); cout << hex << i << endl; cout << sizeof(float) << endl; cout << sizeof(unsigned int) << endl; } --- end code --- run w/o -O2: bernh...@xbernhard$ c++ floatmini.cpp -o floatmini bernh...@xbernhard$ ./floatmini 3f800000 4 4 => works with -O2 bernh...@xbernhard$ c++ floatmini.cpp -o floatmini -O2 bernh...@xbernhard$ ./floatmini 8048779 4 4 => fails * If codeline 8 is active (see comment), works too with -O2. * unsigned int& i = reinterpret_cast<unsigned int&>(f); fails the same way. Is that line not standard conform? ------------------------------------ bernh...@xbernhard$ c++ -v Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --disable-libmudflap --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu Thread model: posix gcc version 4.2.3 (Debian 4.2.3-2) -- Summary: evaluation of reinterpret_cast with -O2 Product: gcc Version: 4.2.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: ElGringo at gmx dot at GCC build triplet: i486-linux-gnu GCC host triplet: i486-linux-gnu GCC target triplet: i486-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38855