C和C ++操作数的解析顺序 [英] C and C++ operand resolution order

查看:98
本文介绍了C和C ++操作数的解析顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多次我看到(有时写)类似于此示例的代码:

Many times I see (and sometimes write) code similar to this example:

int a=0, b=2;
if( a && (b=func())!=0 ) {
//...

问题是:标准是否保证这些声明?

The question is: does the standard guarantee these statements?

  1. b将不会被触摸(并保持值2)
  2. func()将不会被调用
  1. b will be not touched (and remain value 2)
  2. func() will not be called

反之亦然,如果我们写if( func()!=0 && a )-标准保证书func()是否会被调用?

And vice-versa, if we write if( func()!=0 && a ) - does the standard guarantee func() will be called?

我对定义其合法性的特定标准段落感兴趣.

I'm interested in the particular standard paragraph defining this is legitimate.

UPD :我的错字,从int a=1更改为int a=0

UPD: my typo, changed from int a=1 to int a=0

推荐答案

确切的问题;

问题是:标准能保证这些陈述吗?

The question is: does standard guarantee these statements?

到更新的问题;给定a=0.如果a==0,则为是,短路评估将开始,并且不会调用func();否则,将调用func().第二个操作数将不被评估.

To the updated question; given a=0. If a==0, then yes, the short circuit evaluation would kick in and func() would not be called; the second operand would not be evaluated.

如果是a=1(与原来一样),则相反. func()将被调用-a1,因此为"true",结果是第二个操作数被求值(它是逻辑AND),b将发生变化.如果操作员为||(逻辑或),则短路评估会开始,并且不会调用func().

If a=1 (as it was originally), the opposite; func() will be called - a is 1 thus "true", as a result the second operand is evaluated (it is a logical AND), b will change. If the operator had been || (logical OR), then short circuit evaluation would kick in and func() would not be called.

反之亦然,如果我们编写if( func()!=0 && a ) -是否会调用标准保证func()?

And vice-versa, if we write if( func()!=0 && a ) -- does standard guarantee func() will be called?

是的,始终对第一个操作数求值.

Yes, the first operand is always evaluated.

是的,可以保证C ++的短路评估;

Yes, short circuit evaluation is guaranteed for C++;

§5.14逻辑AND运算符

1 &&运算符从左到右分组.操作数均在上下文中转换为布尔值(第4条).如果两个操作数都为true,则结果为true,否则为false.与&不同,&&保证从左到右的求值:如果第一个操作数为false,则不对第二个操作数求值.

1 The && operator groups left-to-right. The operands are both contextually converted to bool (Clause 4). The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.

2结果是布尔值.如果评估了第二个表达式,则在与第二个表达式关联的每个值计算和副作用之前对与第一个表达式关联的每个值计算和副作用进行排序.

2 The result is a bool. If the second expression 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 expression.

§5.15逻辑或运算符

1 ||运算符从左到右分组.操作数均在上下文中转换为布尔值(第4条).如果两个操作数中的任何一个为true,则返回true,否则返回false.与|不同,||保证从左到右的评估;此外,如果第一个操作数的值为true,则不计算第二个操作数.

1 The || operator groups left-to-right. The operands are both contextually converted to bool (Clause 4). It returns true if either of its operands is true, and false otherwise. Unlike |, || guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true.

2结果是布尔值.如果评估了第二个表达式,则在与第二个表达式关联的每个值计算和副作用之前对与第一个表达式关联的每个值计算和副作用进行排序.

2 The result is a bool. If the second expression 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 expression.


C对应的引号是;


The corresponding quotes for C are;

§6.5.13逻辑AND运算符

4与按位二进制&运算符不同,&&运算符保证从左到右的求值;如果对第二个操作数求值,则在第一个和第二个操作数的求值之间有一个序列点.如果第一个操作数比较等于0,则不计算第二个操作数.

4 Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares equal to 0, the second operand is not evaluated.

§6.5.14逻辑或运算符

4与按位|运算符不同,||运算符保证从左到右的求值;如果对第二个操作数求值,则在第一个和第二个操作数的求值之间有一个序列点.如果第一个操作数比较不等于0,则不计算第二个操作数.

4 Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares unequal to 0, the second operand is not evaluated.

这篇关于C和C ++操作数的解析顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆