>>>s[i] = s[--i];
>>As you both use and update "i" in your expression, you have broken
>>the contract with the compiler and it is free to do anything. Not
>>limited to the two behaviours you have seen.
> Are you sure about this? Another poster quoted:
>>4. Except where noted, the order of evaluation of operands of
>> individual operators and subexpressions of individual expressions,
>> and the order in which side effects take place, is unspecified.
>> Between the previous and next sequence point a scalar object
>> shall have its stored value modified at most once by the evaluation
>> of an expression. Furthermore, the prior value shall be accessed
>> only to determine the value to be stored. The requirements of this
>> paragraph shall be met for each allowable ordering of the
>> subexpressions of a full expression; otherwise the behavior is
> This seems to imply that the compiler can choose any ordering it likes
> among the subexpressions, but it can't do the Towers of Hanoi.
Actually, the paragraph you quote gives the implementation
the freedom to do the Towers of Hanoi. That's what the
"the requirements of this paragraph shell be met... ; otherwise
the behavior is undefined" says, speaking of requirements placed
on user code. If you write code such that reads and writes
between sequence points break the rules, all bets are off.
This has been covered many times in the past; the archives of
comp.std.c++ will provide many repetitions, as will those of
any other major C++ newsgroup I know of.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]