D
Danny Woods
Hi all,
I've inherited an unusual piece of code that behaves slightly oddly.
A method on a class mutates the value of an integer instance
variable by bitwise or-ing it with the result of a function that also
(conditionally) manipulates that same piece of state. Reducing it to a
simple test case:
---
#include <iostream>
using std::cout;
using std::endl;
class BitTwiddle
{
private:
int m_field;
public:
BitTwiddle() { m_field = 2; }
int twiddle() { return m_field |= mutate(); }
int mutate() { return m_field = 1; }
};
int main(void)
{
BitTwiddle bt;
cout << "Twiddled: " << bt.twiddle() << endl;
return 0;
}
---
With Visual C++ and Cygwin g++ on Windows, this code produces the string
'Twiddled: 1', but on the Mac and on Linux (both g++), this produces the
string 'Twiddled: 3'.
So the question: what does the Standard have to say about this? Are any
of the compilers wrong, or has this wandered off into 'undefined'
territory?
Cheers,
Danny.
I've inherited an unusual piece of code that behaves slightly oddly.
A method on a class mutates the value of an integer instance
variable by bitwise or-ing it with the result of a function that also
(conditionally) manipulates that same piece of state. Reducing it to a
simple test case:
---
#include <iostream>
using std::cout;
using std::endl;
class BitTwiddle
{
private:
int m_field;
public:
BitTwiddle() { m_field = 2; }
int twiddle() { return m_field |= mutate(); }
int mutate() { return m_field = 1; }
};
int main(void)
{
BitTwiddle bt;
cout << "Twiddled: " << bt.twiddle() << endl;
return 0;
}
---
With Visual C++ and Cygwin g++ on Windows, this code produces the string
'Twiddled: 1', but on the Mac and on Linux (both g++), this produces the
string 'Twiddled: 3'.
So the question: what does the Standard have to say about this? Are any
of the compilers wrong, or has this wandered off into 'undefined'
territory?
Cheers,
Danny.