简体   繁体   English

比较运算符的评估顺序?

[英]order of evaluation of comparison operator?

I have learnt that logical operator are guaranteed that their evaluation are from left-to right but I was wondering what are the order of evaluation of comparison operator. 我了解到逻辑运算符可以保证从左到右进行评估,但是我想知道比较运算符的评估顺序是什么。 For instance expression1 < expression2 in other words is it guaranteed that expression1 will be first evaluated before expression2 . 换句话说,例如expression1 < expression2可以确保在expression1之前先对expression2进行求值。

According to the standard: 根据标准:

J.1 Unspecified behavior J.1未指明的行为

The following are unspecified: 未指定以下内容:
.... ....

— The order in which subexpressions are evaluated and the order in which side effects take place, except as specified for the function-call () , && , || —除了为函数调用()&&||指定的子表达式的计算顺序和副作用发生的顺序 , ?: , and comma operators (6.5). ?:和逗号运算符(6.5)。

Generally speaking, the order of evaluation of subexpressions within an expression is undefined. 一般来说,表达式中子表达式的求值顺序是不确定的。

The only place where there is an order, ie sequence points, is the || 唯一有顺序(即顺序点)的地方是|| (logical OR), && (logical AND), , (comma), and ?: (ternary) operators. (逻辑OR) && (逻辑与) , (逗号),和?: (三元)运算符。

In the case of && , if the expression on the left evaluates to false (ie 0), the result is known to be false and the right side is not evaluated. &&的情况下,如果左侧的表达式的计算结果为false(即0),则已知结果为false,并且不对右侧的结果求值。 Similarly for || 对于||同样 if the expression on the left evaluates to true (ie not 0), the result is known to be true and the right side is not evaluated. 如果左侧的表达式求值为true(即不为0),则结果为true,而右侧的表达式不求值。

For the ternary operator, the conditional is evaluated first. 对于三元运算符,首先评估条件。 If it evaluates to true then only the middle part is evaluated, otherwise only the third part is evaluated. 如果评估结果为true,则仅评估中间部分,否则仅评估第三部分。

For the comma operator, the left side is evaluated first, then the right side. 对于逗号运算符,首先评估左侧,然后评估右侧。

From the C standard : C标准

6.5.13.4 Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; 6.5.13.4与按位二进制&运算符不同,&&运算符保证从左到右的求值; there is a sequence point after the evaluation of the first operand. 在第一个操作数求值之后有一个序列点。 If the first operand compares equal to 0, the second operand is not evaluated. 如果第一个操作数比较等于0,则不评估第二个操作数。

... ...

6.5.14.4 Unlike the bitwise | 6.5.14.4与按位| operator, the || 运算符,|| operator guarantees left-to-right evaluation; 运营商保证从左到右的评估; there is a sequence point after the evaluation of the first operand. 在第一个操作数求值之后有一个序列点。 If the first operand compares unequal to 0, the second operand is not evaluated. 如果第一个操作数比较不等于0,则不计算第二个操作数。

... ...

6.5.15.4 The first operand is evaluated; 6.5.15.4计算第一个操作数; there is a sequence point after its evaluation. 评估后有一个序列点。 The second operand is evaluated only if the first compares unequal to 0; 仅当第一个操作数不等于0时,才对第二个操作数求值; the third operand is evaluated only if the first compares equal to 0; 仅当第一个操作数等于0时,才计算第三个操作数; the result is the value of the second or third operand (whichever is evaluated), converted to the type described below. 结果是第二个或第三个操作数的值(以所评估的为准),转换为以下所述的类型。 If an attempt is made to modify the result of a conditional operator or to access it after the next sequence point, the behavior is undefined. 如果试图修改条件运算符的结果或在下一个序列点之后访问它,则行为是不确定的。

.... ....

6.5.17.2 The left operand of a comma operator is evaluated as a void expression; 6.5.17.2逗号运算符的左操作数被评估为void表达式; there is a sequence point after its evaluation. 评估后有一个序列点。 Then the right operand is evaluated; 然后评估正确的操作数; the result has its type and value. 结果具有其类型和价值。 If an attempt is made to modify the result of a comma operator or to access it after the next sequence point, the behavior is undefined. 如果试图修改逗号运算符的结果或在下一个序列点之后访问它,则行为是不确定的。

No, the spec does not mention the order of evaluation for the operand of relational operators. 不,该规范提及关系运算符的操作数的评估顺序 It's unspecified. 未指定。

Just to add, relational operators are left-to-right associative . 只需补充一下, 关系运算符是从左到右的关联

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

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