“IF” argument evaluation order?

前端 未结 6 867
小鲜肉
小鲜肉 2020-12-02 18:17
if(a && b)
{
  do something;
}

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

if \"yes\", what influenc

相关标签:
6条回答
  • 2020-12-02 18:51

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

    • operator && evaluates left operand first and if the value is logically false then 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 true then it avoids evaluating the right operand. For example if (overwrite_files || confirm("File existing, overwrite?")) ... will not ask confirmation when the flag overwrite_files is 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 not comma operators and the order of evaluation is not guaranteed.

    • The ternary operator x?y:z evaluates x first, and then depending on the logical value of the result evaluates either only y or 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).

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

    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.

    0 讨论(0)
  • 2020-12-02 18:59

    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.

    If you want b to be evaluated first:

    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
    }
    
    0 讨论(0)
  • 2020-12-02 19:01

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

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

    If a is false, then b will not be evaluated.

    If you want b to be evaluated first, and a only if b is true, simply write the expression the other way around:

    if (b && a)
    {
       //block of code
    }
    
    0 讨论(0)
  • 2020-12-02 19:11

    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.

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

    0 讨论(0)
  • 2020-12-02 19:11

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

    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.

    0 讨论(0)
  • 2020-12-02 19:12

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

    If a returns false, then b is not allowed to evaluate at all.

    0 讨论(0)
提交回复
热议问题