简体   繁体   English

C ++ 11赋值表达式是从右到左求值的吗?

[英]Is a C++11 assignment expression evaluated right to left?

The order of evaluation of an expression has always been a source of unspecified behaviors in C++. 表达式的求值顺序一直是C ++中未指定行为的来源。 Has the C++11 standard finally stated what the order of evaluation should be? C ++ 11标准是否最终规定了评估的顺序?

Do we now know the values of var1 and var2 of the following expression: 现在我们是否知道以下表达式的var1var2的值:

int var1 =10, var2=20;
var1 = var2 = 30;

Will it be var1=30 and var2=30 , or var1=20 and var2=30 ? var1=30 and var2=30 ,还是var1=20 and var2=30

No, the new standard does not specify a sequencing or ordering of evaluations of all subexpressions. 不可以,新标准未指定所有子表达式的评估顺序排序。

The expression a + b + c is grouped grammatically as (a + b) + c , but the three subexpressions a , b and c can be evaluated in any order and the evaluations are not sequenced with respect to each other. 表达式a + b + c在语法上被分组为(a + b) + c ,但是三个子表达式abc可以按任意顺序求值,并且求值没有相对于彼此排序。

To make this more concrete, consider: 为了更具体一点,请考虑:

int main()
{
    return printf("Hello") + printf("World") + printf("\n");
}

As for your code: There is no ambiguity there. 至于您的代码:那里没有歧义。 It is one expression, an assignment expression of the form a = b , where a is the lvalue var1 and b is the subexpression var2 = 30 . 它是一个表达式,形式为a = b的赋值表达式,其中a是左值var1b是子表达式var2 = 30 The fact that you're wondering whether var1 ends up as 20 or as 30 leads me to believe that you're unsure about the operator associativity (for = ). 您想知道var120还是30的事实使我相信您不确定运算符的关联性(for = )。 That, however, has never been ambiguous and is perfectly well specified in all language variants I can think of. 但是,这从来都不是模棱两可的,并且在我能想到的所有语言变体中都得到了很好的说明。 The assigment operator associates on the right, leading to the subexpressions a and b that I have described. 分配运算符在右侧关联,导致我已经描述了子表达式ab This (extremely fundamental) aspect of the language has not been changed in C++11. 在C ++ 11中,语言的这一方面(极其基础)没有更改。

If you really want to combine the two problems, you should consider the following expression: 如果您确实想将两个问题结合在一起,则应考虑以下表达式:

var1 = 10;
(var1 = 20) = (var2 = var1);

Now the final expression is also a = b , but both a and b are non-trivial subexpression whose evaluation is not ordered. 现在,最终表达式也是a = b ,但是ab都是非平凡的子表达式,其求值没有顺序。

var1 = var2 = 30;

Both shall be 30 and it's specified by standard. 两者均应为30 ,并由标准指定。 The thing which is not specified is, if assigning operands are complex expression which must be evaluated before assigment, what's the sequence of evaluating them? 没有指定的是,如果赋值操作数是必须在赋值之前求值的复杂表达式,则求值顺序是什么?

(expr1) = (expr2) = x;
   1         2

or

(expr1) = (expr2) = x;
   2         1

The assignment operator (=) and the compound assignment operators all group right-to-left. 赋值运算符(=)和复合赋值运算符均从右到左分组。

This doesn't tell us anything about evaluation order. 这没有告诉我们有关评估顺序的任何信息。 It merely means that a = b = c is parsed as a = (b = c) and not (a = b) = c . 这仅意味着将a = b = c解析为a = (b = c)而不是(a = b) = c

1.9/15 still applies: 1.9/15仍然适用:

Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. 除非另有说明,否则不对单个运算符的操作数和单个表达式的子表达式求值。

Sequencing rules introduce partial ordering only. 排序规则仅引入部分排序。 In an expression like this: 在这样的表达式中:

(x+42) = (y+42)

it is guaranteed that both subexpressions (x+42) and (y+42) are executed before the assignment to the result of (x+42) occurs, but the two subexpressions themselves are not sequenced. 确保在对(x+42)的结果进行赋值之前都执行了两个子表达式(x+42)(y+42) ,但是两个子表达式本身未排序。 Either can be executed before the other one and they can even be interleaved and the order need not be consistent during the execution of the program. 任一个都可以在另一个执行之前执行,甚至可以交错执行,并且在执行程序期间顺序不必保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM