函数调用中表达式的求值顺序

给定一个函数调用func(E1, E2, E3),其中 E1 等是任意表达式,每个表达式相对于每个其他表达式是不确定的,还是所有表达式都是未排序的(即评估可以重叠)?

我看过这个cppreference 页面,它在规则 15 中使用了这句话

在函数调用中,每个参数初始化的值计算和副作用相对于任何其他参数的值计算和副作用是不确定的。

我认为这与我要问的不太一样,因为参数的初始化只是参数表达式评估的最后一步。

但是讨论其他内容的规则 21 似乎暗示函数调用中的每个子表达式都是不确定的

括号中的初始值设定项中以逗号分隔的表达式列表中的每个表达式都像函数调用一样求值(不确定顺序)

所以我有点困惑,任何指导表示赞赏。

回答

C++17 状态在

8.2.2 函数调用【expr.call】

4 ... The initialization and destruction of each parameter occurs within
the context of the calling function.


5 ... Note: All side effects of argument evaluations are sequenced before the
function is entered


5 ... The initialization of a parameter, including every associated value
computation and side effect, is indeterminately sequenced with respect
to that of any other parameter.

I hope this (my bolding) is clear enough.

(ref: n4659, final C++17 draft)

  • @VioletGiraffe Thank you, I am aware. However, it is possible to obtain free very-close-to-publication draft versions of each standard, or some may actually pay for the standard. Regardless, the current working draft (at time of writing) is some three years newer than the standard the OP is asking about, which may well yield differences in these rules. In short: citing a C++20 draft is no proof of C++17 rules.

以上是函数调用中表达式的求值顺序的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>