I ran into strange behavior in VC++2010, and I think it's a compiler bug, unless I'm missing something obvious:
class A
{
public:
A& B(int& value)
{
value = 10;
return *this;
}
A& C(int value)
{
printf("%d\r\n", value);
return *this;
}
A& D(int value1, int value2)
{
printf("%d, %d\r\n", value1, value2);
return *this;
}
}
int main()
{
A a;
int x = 0; // get uninitialized variable warning if I don't assign x
a.B(x).C(x).D(x, x - 1);
}
I would expect:
10
10, 9
But it actually prints:
10
10, -1
It's obviously assuming x can't be modified by any of the prior function calls and replaces "x - 1" with "-1".
Is this somehow invoking undefined behavior, and thus acting unexpectedly, or is it a compiler bug?