Compiler Optimization & Increments
Does compiler optimization cause a problem with code like this? Specifically, can the auto-increments be replied upon to evaulate in the correct order?
uint result =开发者_StackOverflow (array[i++] << 16) | (array[i++] << 8) | array[i++];
Yes; this is specified.
The spec says:
Operands in an expression are evaluated from left to right. For example, in
F(i) + G(i++) * H(i)
, methodF
is called using the old value ofi
, then methodG
is called with the old value ofi
, and, finally, methodH
is called with the new value ofi
. This is separate from and unrelated to operator precedence.
No, I think you're modifying the object (i) multiple times between sequence points. IIRC neither bitwise or, nor array indexing are sequence points. You are only allowed to modify the object once, doing otherwise is unspecified or undefined behaviour.
NOTE. This is not necessarily an issue to do with the optimizer, it's just not legal.
Edit: Here's a link on sequence points - http://msdn.microsoft.com/en-us/library/d45c7a5d(VS.80).aspx
精彩评论