C#: Order of function evaluation (vs C)

后端 未结 8 537
旧时难觅i
旧时难觅i 2020-12-19 07:08

Take the following C code (K&R pg. 77) :

push(pop() - pop()); /* WRONG */

The book says that since - and /

8条回答
  •  隐瞒了意图╮
    2020-12-19 07:49

    Order of evaluation is well-defined in C# in all cases, and is left-to-right. From C# language spec (§7.3):

    The order of evaluation of operators in an expression is determined by the precedence and associativity of the operators (§7.2.1). Operands in an expression are evaluated from left to right. For example, in F(i) + G(i++) * H(i), method F is called using the old value of i, then method G is called with the old value of i, and, finally, method H is called with the new value of i. This is separate from and unrelated to operator precedence

    In case of C++, it's not that the order couldn't be defined; it's that allowing the order to be undefined allows the compiler to better optimize code.

提交回复
热议问题