CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2007
    Location
    Calcutta, India
    Posts
    95

    [RESOLVED] order of evaluation of condition expression.

    Hi all,

    I have written some production level code assuming that if there is something like

    Code:
    if ( condition A || condition B || condition C)
    
    or 
    
    if ( condition A && condition B && condition C)
    the conditions will be evaluated left to right. i.e. A first, B next and C last.

    Someone who reviewed my code from Ireland has commented that the standards don't guarantee any such thing.

    I have found this in the K&R, which says: C does not guarantee the order of evaluation of expressions other than for ||, &&, comma and the ?: operators.

    Can someone who is conversant with the current standards let me know if I am right or wrong?

    I

  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: order of evaluation of condition expression.

    Logical operators, WHEN NOT OVERLOADED, have some special property: they are evaluated left to right, and when evaluation yields the result which will be not affected by following expressions, it is terminated. So when you have something like this:

    Code:
    T* object = getObject(); //returns NULL on failure
    if(object && object->getSomeProperty() == "value") {
       //...
    }
    it is guarenteed that program will not try to invoke member function, if pointer points to NULL. It is called short-circuit evaluation and occurs only for non-overloaded operators && and ||.
    Comma and conditional operators also have guaranted order of expression evaluation, unless overloaded.
    Remaining operators, either overloaded or not, have unspecified order of subexpression evaluation.

    Cheers
    Last edited by Hobson; November 17th, 2008 at 05:34 AM.
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: order of evaluation of condition expression.

    The operators || and && are "short-circuit" operators. This means that as soon as the result is guaranteed (working from left to right), it stops evaluating. Essentially it means that this
    Code:
    if (ptr && ptr->mem_fun())
    is safe - if ptr == 0, then it will not attempt to dereference it.

    And yes, the comma and ternary operators behave similarly.

    Here's the entry for the logical AND operator from the standard:
    Quote Originally Posted by C++ standard 5.14
    1. The && operator groups left-to-right. The operands are both implicitly converted to type bool (clause 4). The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.

    2. The result is a bool. All side effects of the first expression except for destruction of temporaries (12.2) happen before the second expression is evaluated.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: order of evaluation of condition expression.

    Both C++03 and C99 guarantee left-to-right evaluation for || and &&. If you want to quote them:
    Quote Originally Posted by C++03 Section 5.14 Paragraph 1
    The && operator groups left-to-right. The operands are both implicitly converted to type bool (clause 4). The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.
    Quote Originally Posted by C++03 Section 5.15 Paragraph 1
    The || operator groups left-to-right. The operands are both implicitly converted to bool (clause 4). It returns true if either of its operands is true, and false otherwise. Unlike |, || guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true.
    Quote Originally Posted by C99 Section 6.5.13 Paragraph 4
    Unlike the bitwise binary & 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 equal to 0, the second operand is not evaluated.
    Quote Originally Posted by C99 Section 6.5.14 Paragraph 4
    Unlike the bitwise | 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Sep 2007
    Location
    Calcutta, India
    Posts
    95

    Re: order of evaluation of condition expression.

    Thanks a LOT Graham and particularly laserlight for giving me the exact references.
    I

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: [RESOLVED] order of evaluation of condition expression.

    Just remember (as previously pointed out), the quoted parts of the standard ONLY apply for "non-overloaded" operators. This means that if you create your own "operator &&" or ""operator ||" you will NOT have the short-circuit behaviour.

    So without knowing the TYPES of "ConditionA", "ConditionB" and "ConditionC" it is actually impossible to tell if the ordering and short-circuit apply...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: [RESOLVED] order of evaluation of condition expression.

    And this is why Meyers recommends never overloading logical AND, logical OR or comma (you can't overload the ternary operator).
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Sep 2007
    Location
    Calcutta, India
    Posts
    95

    Re: [RESOLVED] order of evaluation of condition expression.

    Meyer's is....can't find words ( all good words fall short of describing his metel).
    I

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured