Very interesting. The C++ Standard says, ".. the order of evaluation of operands of individual operators ... and the order in which side effects take place is unspecified."
And gives the example:
i = ++i + 1; // the behaviour is unspecified
So, you're doing nothing wrong. C++ does not specify the order of all the ++, -- stuff and can do it in any order.
My G++ with no oprimisations gives: 35
My Visual Studio 2005 with no optimisations gives: 33
It's a trick question, but there's a lession in there somewhere.
Actually, the behavior for kbw's example is defined. It's the same as
1 2
i++;
i=i+1;
What the standard means to say is this:
When there is more than one side effect for the same variable in the same expression, the order in which these side effects will be executed is undefined.
Example: a=a++ + a++ + a++;
VC++ will understand this as
1 2 3 4
a=a+a+a;
a++;
a++;
a++;
GCC will understand this as
1 2 3 4 5 6
a=a;
a++;
a+=a;
a++;
a+=a;
a++;
This can get pretty messy. In fact, I think right now I have a bug to eliminate because of this undefined behavior.