C++ Order of Evaluation of Subexpressions with Logical Operators

梦想与她 提交于 2020-02-28 06:34:05

问题


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.

Consider the following statement:

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

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

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

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); // 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.

There is also another mention of this in section 11.1.1:

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;

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.


回答1:


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".

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


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.




回答2:


Yes, it is guaranteed that f(0) will be completely evaluated first.

This is to support behaviour known as short-circuiting, by which we don't need to call the second function at all if the first returns false.



来源:https://stackoverflow.com/questions/31101327/c-order-of-evaluation-of-subexpressions-with-logical-operators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!