C++ “IF”参数评估顺序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7925479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:43:44  来源:igfitidea点击:

"IF" argument evaluation order?

c++if-statementoperator-precedence

提问by winnerrrr

if(a && b)
{
  do something;
}

is there any possibility to evaluate arguments from right to left(b -> a)?

是否有可能从右到左评估参数(b -> a)?

if "yes", what influences the evaluation order?

如果“是”,什么影响评估顺序?

(i'm using VS2008)

(我使用的是 VS2008)

回答by 6502

With C++ there are only a few operators that guarantee the evaluation order

使用 C++,只有少数运算符可以保证评估顺序

  • operator &&evaluates left operand first and if the value is logically falsethen it avoids evaluating the right operand. Typical use is for example if (x > 0 && k/x < limit) ...that avoids division by zero problems.

  • operator ||evaluates left operand first and if the value is logically truethen it avoids evaluating the right operand. For example if (overwrite_files || confirm("File existing, overwrite?")) ...will not ask confirmation when the flag overwrite_filesis set.

  • operator ,evaluates left operand first and then right operand anyway, returning the value of right operand. This operator is not used very often. Note that commas between parameters in a function call are notcomma operators and the order of evaluation is not guaranteed.

  • The ternary operator x?y:zevaluates xfirst, and then depending on the logical value of the result evaluates either only yor only z.

  • operator &&首先评估左操作数,如果该值是逻辑值,false则避免评估右操作数。例如if (x > 0 && k/x < limit) ...,典型用途是避免除以零问题。

  • operator ||首先评估左操作数,如果该值是逻辑值,true则避免评估右操作数。例如,设置if (overwrite_files || confirm("File existing, overwrite?")) ...标志时不会要求确认overwrite_files

  • operator ,无论如何先计算左操作数,然后计算右操作数,返回右操作数的值。此运算符不经常使用。请注意,函数调用中参数之间的逗号不是逗号运算符,并且不保证求值顺序。

  • 三元运算符首先x?y:z计算x,然后根据结果的逻辑值计算 onlyy或 only z

For all other operators the order of evaluation is not specified.

对于所有其他运算符,未指定求值顺序。

The situation is actually worse because it's not that the order is not specified, but that there is not even an "order" for the expression at all, and for example in

情况实际上更糟,因为不是没有指定顺序,而是根本没有表达式的“顺序”,例如在

std::cout << f() << g() << x(k(), h());

it's possible that functions will be called in the order h-g-k-x-f(this is a bit disturbing because the mental model of <<operator conveys somehow the idea of sequentiality but in reality respects the sequence only in the order results are put on the stream and not in the order the results are computed).

函数可能会按顺序调用h-g-k-x-f(这有点令人不安,因为<<运算符的心智模型以某种方式传达了顺序性的概念,但实际上仅按照将结果放入流中的顺序而不是按顺序来考虑顺序计算结果)。

Obviously the value dependencies in the expression may introduce some order guarantee; for example in the above expression it's guaranteed that both k()and h()will be called before x(...)because the return values from both are needed to call x(C++ is not lazy).

显然表达式中的值依赖可能会引入一些顺序保证;例如,在上面的表达式中,可以保证k()h()都将在之前被调用,x(...)因为需要调用两者的返回值x(C++ 不是惰性的)。

Note also that the guarantees for &&, ||and ,are valid only for predefined operators. If you overload those operators for your types they will be in that case like normal function calls and the order of evaluation of the operands will be unspecified.

另请注意,&&||和的保证,仅对预定义的运算符有效。如果您为您的类型重载这些运算符,在这种情况下,它们将与普通函数调用一样,并且操作数的计算顺序将是未指定的。

回答by Luchian Grigore

The evaluation order is specified by the standard and is left-to-right. The left-most expression will always be evaluated first with the &&clause.

评估顺序由标准指定,为left-to-right。最左边的表达式总是先用&&子句求值。

If you want bto be evaluated first:

如果你想b先被评估:

if(b && a)
{
  //do something
}

If both arguments are methods and you want both of them to be evaluated regardless of their result:

如果两个参数都是方法,并且您希望无论结果如何都对它们进行评估:

bool rb = b();
bool ra = a();

if ( ra && rb )
{
  //do something
}

回答by Mysticial

In this case, since you're using &&, awill always be evaluated first because the result is used to determine whether or not to short-circuit the expression.

在这种情况下,由于您使用的是&&,a将始终首先评估,因为结果用于确定是否短路表达式。

If areturns false, then bis not allowed to evaluate at all.

如果a返回 false,则b根本不允许进行评估。

回答by Pacheco

Every value computation and side effect of the first (left) argument of the built-in logical AND operator && and the built-in logical OR operator || is sequenced before every value computation and side effect of the second (right) argument.

内置逻辑 AND 运算符 && 和内置逻辑 OR 运算符 || 的第一个(左)参数的每个值计算和副作用 在第二个(右)参数的每个值计算和副作用之前排序。

Read here for a more exhaustive explanation of the rules set: order evaluation

阅读此处以获取对规则集的更详尽解释: 订单评估

回答by Dylan Smith

It will evaluate from left to right and short-circuit the evaluation if it can (e.g. if a evaluates to false it won't evaluate b).

它将从左到右求值,并在可以的情况下短路求值(例如,如果 a 求值为 false,则不会求值 b)。

If you care about the order they are evaluated in you just need to specify them in the desired order of evaluation in your if statement.

如果您关心它们的评估顺序,您只需要在 if 语句中以所需的评估顺序指定它们。

回答by sameer

The built-in &&operator always evaluates its left operand first. For example:

内置&&运算符始终首先评估其左操作数。例如:

if (a && b)
{
   //block of code
}

If ais false, then bwill not be evaluated.

如果afalse,则b不会被评估。

If you want bto be evaluated first, and aonly if bis true, simply write the expression the other way around:

如果您想b首先被评估,并且a仅当b为真时,只需将表达式反过来写:

if (b && a)
{
   //block of code
}