Originally Posted by ankitm
This is not just an order of operations issue.
C++ does not specify which sequence the parameters are computed in, so any change in the way you compile could change that sequence (optimization level, compiler version, platform, phase of the moon).
But the problem is more serious than that. Multiple changes to the same variable in one expression is expressly undefined behavior. You not only have no reasonable expection of what sequence i++, --i and i-- will occur in, you have no reasonable expectation that they will all even occur. The value of i when this expression completes is unpredictable.
If this were just an order of evaluation issue, the three values passed to printf would be unpredictable within a very narrow range, while the resulting value of i would be reliably reduced by a net 1. In actual C++ the values passed are unpredictable across a wider range and the resulting value of i is also unpredictable.
The standard goes even further in specifying that use of an undefined construct make the entire behavior of the whole program undefined. You can't rely on what three values get passed to printf. You can't rely on what value ends up in i. You also can't rely on anything else done by any part of your program. In my opinion
, the first two of those are very real concerns in a case like this and the third is silly. But the standard says all three should be concerns.