Bug 27955 - friend members produce different results
friend members produce different results
Status: RESOLVED DUPLICATE of bug 21920
Product: gcc
Classification: Unclassified
Component: c++
3.4.4
: P3 normal
: ---
Assigned To: Not yet assigned to anyone
:
Depends on:
Blocks:
  Show dependency treegraph
 
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:


Attachments

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 

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;
}
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 ***