This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
value not set via reference
- From: Conrad S <conradsand dot arma at gmail dot com>
- To: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Fri, 30 Jan 2015 16:38:00 +1000
- Subject: value not set via reference
- Authentication-results: sourceware.org; auth=none
Which compiler is correct here - gcc or clang?
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64870
Consider the following code:
#include <iostream>
struct blah {
inline double setval(unsigned int& x) const
{
x = 123;
return 456.0;
}
};
int
main(int argc, char** argv) {
blah blah_instance;
unsigned int val = 9999;
std::cout << blah_instance.setval(val) << " val: " << val << std::endl;
std::cout << blah_instance.setval(val) << " val: " << val << std::endl;
return 0;
}
when compiled with gcc 4.9.2, the above program produces:
456 val: 9999 <-- unexpected
456 val: 123
when compiled with clang 3.5:
456 val: 123
456 val: 123
Clang has the least surprising result. Is gcc relying on a loophole in
C++ legalese to muck up the order of evaluation?