gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:05:08 25 4
gpt4 key购买 nike

给定一个函数调用 func(E1, E2, E3)其中 E1 等是任意表达式,每个表达式相对于其他表达式的顺序是否不确定,还是所有表达式都未排序(即评估可以重叠)?
我看过 cppreference page在这个和它,在规则 15 中,使用句子

In a function call, value computations and side effects of theinitialization of every parameter are indeterminately sequenced withrespect to value computations and side effects of any other parameter.


我认为这与我要问的不太一样,因为参数的初始化只是评估参数表达式的最后一步。
但是讨论其他内容的规则 21 似乎暗示函数调用中的每个子表达式都是不确定的

Every expression in a comma-separated list of expressions in aparenthesized initializer is evaluated as if for a function call(indeterminately-sequenced)


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

最佳答案

C++17 状态在
8.2.2 函数调用【expr.call】

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



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



5 ... The initialization of a parameter, including every associated valuecomputation and side effect, is indeterminately sequenced with respectto that of any other parameter.


我希望这(我的粗体)足够清楚。
(引用: n4659, final C++17 draft )

关于c++ - 函数调用中表达式的求值顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65281525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com