用逻辑运算符对子表达式求值的C ++顺序 [英] C++ Order of Evaluation of Subexpressions with Logical Operators

查看:119
本文介绍了用逻辑运算符对子表达式求值的C ++顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于优先级评估顺序的概念有很多疑问,但我找不到与我的特殊情况有关的问题.

There are lots of questions on concepts of precedence and order of evaluation but I failed to find one that refers to my special case.

考虑以下语句:

if(f(0) && g(0)) {};

保证会首先评估f(0)吗?请注意,运算符是&&.

Is it guaranteed that f(0) will be evaluated first? Notice that the operator is &&.

我的困惑源于我在"The C ++ Programming Language,(Stroustrup,4ed,2013)"中阅读的内容.

My confusion stems from what I've read in "The C++ Programming Language, (Stroustrup, 4ed, 2013)".

这本书的10.3.2节说:

In section 10.3.2 of the book, it says:

表达式中子表达式的求值顺序是未定义.特别是,您不能假定该表达式是从左到右求值的.例如:

The order of evaluation of subexpressions within an expression is undefined. In particular, you cannot assume that the expression is evaluated left-to-right. For example:

int x = f(2)+ g(3); //不确定是先调用f()还是g()

int x = f(2)+g(3); // undefined whether f() or g() is called first

这似乎适用于所有运营商,包括和&&运算符,但在以下段落中表示:

This seems to apply to all operators including && operator, but in a following paragraph it says:

运算符,(逗号),&& (逻辑与)和|| (逻辑或)保证,先评估其左侧操作数,再评估其右侧操作数.

The operators , (comma), && (logical and), and || (logical or) guarantee that their left-hand operand is evaluated before their right-hand operand.

在11.1.1节中还另外提到了这一点:

There is also another mention of this in section 11.1.1:

&&和||运算符仅在必要时才对第二个参数进行求值,因此可以将它们用于控制求值顺序(第10.3.2节).例如:

The && and || operators evaluate their second argument only if necessary, so they can be used to control evaluation order (§10.3.2). For example:

while(p&!whitespace(p))++ p;

while (p && !whitespace(p)) ++p;

在这里,如果p是nullptr,则不会取消引用.

Here, p is not dereferenced if it is the nullptr.

最后一个引号表示&&和||首先评估他们的第一个参数,因此似乎加强了我的假设,即第二个引号中提到的运算符是第一个引号的例外,但我也不能从最后一个例子中得出明确的结论,因为该表达式仅包含一个子表达式,而不是我的示例,其中包含两个.

This last quote implies that && and || evaluate their 1st argument first, so it seems to reinforce my assumption that operators mentioned in 2nd quote are exceptions to 1st quote, but I cannot draw a definitive conclusion from this last example either, as the expression contains only one subexpression as opposed to my example, which contains two.

推荐答案

&&||,的特殊排序行为在C和C ++中已得到很好的确立.您引用的第一句话应为通常未指定表达式中子表达式的求值顺序"或除少数特定例外情况之外,未指定表达式中子表达式的求值顺序".

The special sequencing behavior of &&, ||, and , is well-established in C and C++. The first sentence you quoted should say "The order of evaluation of subexpressions within an expression is generally unspecified" or "With a few specific exceptions, the order of evaluation of subexpressions within an expression is unspecified".

您问过有关C ++的问题,但此问题 http://c-faq.com/expr/seqpointops.html"rel =" nofollow noreferrer> C常见问题解答列表是相关的.

You asked about C++, but this question in the C FAQ list is pertinent.

附录:我刚刚意识到,在这些规则中,未指定"比未定义"更好.编写类似f() + g()的东西不会给您未定义的行为.您只是无法知道先调用f还是g.

Addendum: I just realized that "unspecified" is a better word in these rules than "undefined". Writing something like f() + g() doesn't give you undefined behavior. You just have no way of knowing whether f or g might be called first.

这篇关于用逻辑运算符对子表达式求值的C ++顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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