gpt4 book ai didi

C++ 嵌套条件运算符的求值顺序

转载 作者:太空宇宙 更新时间:2023-11-04 15:38:22 27 4
gpt4 key购买 nike

对于像这样的表达式

x = a ? b : c ? d : e;

我理解因为 ?: 运算符具有正确的结合性,所以表达式被分组为

x = a ? b : (c ? d : e);

但是,评估的顺序呢?结合性是否意味着 (c ? d : e) 分支首先求值,然后它的答案作为参数传递给左边的 ?: 运算符?还是先评估 a,然后根据返回 b 或评估 (c ? d : e) 分支?还是未定义?

最佳答案

n3376 5.16/1

Conditional expressions group right-to-left. The first expression is contextually converted to bool (Clause 4). It is evaluated and if it is true, the result of the conditional expression is the value of the second expression, otherwise that of the third expression. Only one of the second and third expressions is evaluated. Every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second or third expression.

关于C++ 嵌套条件运算符的求值顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27942835/

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