User account creation filtered due to spam.

Bug 27955 - friend members produce different results
Summary: friend members produce different results
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.4
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2006-06-08 11:31 UTC by Julien Durand
Modified: 2006-06-08 14:23 UTC (History)
62 users (show)

See Also:
Host: i686-gnu-linux2.6
Target: i686-gnu-linux2.6
Build: i686-gnu-linux2.6
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Julien Durand 2006-06-08 11:31:52 UTC
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 

g++ -O2 test.cpp -o test

(see below the source code)

is it a mistake on my side or a known/expected behaviour ?



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;
	load(new A(), testValue);
	load(new B(), testValue);
	std::cout<<testValue<<std::endl; //output a different value!!!!
	return 0;
Comment 1 Andrew Pinski 2006-06-08 14:23:08 UTC
                reinterpret_cast<unsigned int&>(t) = 1077936128; //binary float
        reinterpret_cast<unsigned int&>(t) = 1077936128; //binary float value

You are violating C/C++ aliasing rules as you are accessing a float as int.

*** This bug has been marked as a duplicate of 21920 ***