I've encoutered a problem with a friend function embedded into a class declaration AND with the optimisation flag -O2. I have discovered that two successive identical call to cout<<value; in the main program do not yield the same value!!! removing the function definition from the declaration solve the problem, as well as compiling with no optimisation.
tested on two different machines and two different g++ versions: configuration A: machine: Intel Centrino (IA32) OS : windows XP professional compiler : g++ 3.4.4 for cygwin configuration B: machine: Intel Pentium IV (IA32) OS : linux fedora core 5 (2.6) compiler : g++ 4.1.1 command: g++ -O2 test.cpp -o test (see below the source code) is it a mistake on my side or a known/expected behaviour ? cheers, Julien source code: #include <iostream> class A{ friend void load (A* a, float& t){ reinterpret_cast<unsigned int&>(t) = 1077936128; //binary float value of 3 } }; class B{ friend void load (B* b, float& t); }; void load (B* b, float& t) { reinterpret_cast<unsigned int&>(t) = 1077936128; //binary float value of 3 } int main(){ float testValue = -1; //OK load(new A(), testValue); std::cout<<testValue<<std::endl; std::cout<<testValue<<std::endl; //FAIL load(new B(), testValue); std::cout<<testValue<<std::endl; std::cout<<testValue<<std::endl; //output a different value!!!! return 0; } -- Summary: friend members produce different results Product: gcc Version: 3.4.4 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: julien dot durand dot 1981 at gmail dot com GCC build triplet: i686-gnu-linux2.6 GCC host triplet: i686-gnu-linux2.6 GCC target triplet: i686-gnu-linux2.6 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27955