i = i++;后增量操作的结果是增量之前的变量值。
执行i++的后增量操作之后,再次引用i的时候变量i的值为2,但用i++的表达式对i进行重新赋值时,表达式的值为增量操作之前的初值1,即再次将变量i的值赋值为1,所以是个死循环。
i = ++i;前增量操作的结果和变量值是一致的。
执行i++的后增量操作之后,再次引用i的时候变量i的值为2,再用++i的表达式对i进行重新赋值时,表达式的值为2与变量的值保持一致即用变量本身的值对自己赋值,所以能自增
摘自C++ 98标准
5 Expressions
....
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.53) 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 undefined. [Example:
i = v[i++]; // the behavior is unspecified
i = 7, i++, i++; // i becomes 9
i = ++i + 1; // the behavior is unspecified
i = i + 1; // the value of i is incremented
—end example]