CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Sep 2010
    Posts
    27

    Unhappy WHich one is evaluated first

    Given
    !=
    ==
    &&

    which are used within one conditional clause, how can I find out which one will be evaluated first ?

    e.

    if(c==a!=b&&c!=b)
    //dosomething

    Thank you

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: WHich one is evaluated first

    Operators:Precedence of operators : http://www.cplusplus.com/doc/tutorial/operators/
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Sep 2010
    Posts
    27

    Re: WHich one is evaluated first

    thank monarchy

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: WHich one is evaluated first

    Use parentheses to enforce the order so that no one else has to try to remember the precedence when they look at your code.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

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

    Re: WHich one is evaluated first

    Just a warning: "which one will be evaluated first" has to do with order of evaluation, which is sometimes a consequence of the grouping of subexpressions (and thus precedence), but generally the rules of precedence are specified, whereas order of evaluation is generally unspecified.
    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

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: WHich one is evaluated first

    Also, be ware of short-circuit:

    the (non-overloaded) operator&& and operator|| will NOT evaluate the second parameter if the first is false or true respectively. This is because it is useless. (false && x is always false, true || x is always true).

    This is a very know behavior that is not only useful, but relied on.

    Code:
    int * p = 0;
    
    ...
    
    if (p && p->is_valid())
        ...
    if p->is_valid was evaluated, but p was null, the consequences would be catastrophic.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: WHich one is evaluated first

    Quote Originally Posted by monarch_dodra View Post
    Also, be ware of short-circuit:

    the (non-overloaded) operator&& and operator|| will NOT evaluate the second parameter if the first is false or true respectively. This is because it is useless. (false && x is always false, true || x is always true).

    This is a very know behavior that is not only useful, but relied on.

    Code:
    int * p = 0;
    
    ...
    
    if (p && p->is_valid())
        ...
    if p->is_valid was evaluated, but p was null, the consequences would be catastrophic.
    Very true, but I'm sure you meant to declare p as some class type rather than int...
    Old Unix programmers never die, they just mv to /dev/null

  8. #8
    Join Date
    Sep 2010
    Posts
    27

    Re: WHich one is evaluated first

    Quote Originally Posted by monarch_dodra View Post
    Also, be ware of short-circuit:

    the (non-overloaded) operator&& and operator|| will NOT evaluate the second parameter if the first is false or true respectively. This is because it is useless. (false && x is always false, true || x is always true).

    This is a very know behavior that is not only useful, but relied on.

    Code:
    int * p = 0;
    
    ...
    
    if (p && p->is_valid())
        ...
    if p->is_valid was evaluated, but p was null, the consequences would be catastrophic.
    I thank Monachy I learn new things

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: WHich one is evaluated first

    Quote Originally Posted by HighCommander4 View Post
    Very true, but I'm sure you meant to declare p as some class type rather than int...
    Lol. Nice catch sir.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  10. #10
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: WHich one is evaluated first

    Quote Originally Posted by monarch_dodra View Post
    the (non-overloaded) operator&& and operator|| will NOT evaluate the second parameter if the first is false or true respectively.
    Let me get this right ... that does not apply to overloaded && operators then.

    Thinking about how it would be used, with two parameters (or 'this' and one parameter), the right hand parameter would have to be evaluated before the operator could be called.

    I suppose you'd only ever overload it if you did not want the default && behaviour, but it still seems a shame that you cannot keep the 'lazy and' characteristics.

    For smart pointers you could overload the boolean operator instead, but when would you ever want to overload && or || in real code ?

    Quote Originally Posted by HighCommander4 View Post
    Very true, but I'm sure you meant to declare p as some class type rather than int...
    Then again INT could have been anything.
    My hobby projects:
    www.rclsoftware.org.uk

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: WHich one is evaluated first

    Quote Originally Posted by Zaccheus View Post
    Let me get this right ... that does not apply to overloaded && operators then.
    Scott Meyers recommends against overloading those operators for this reason.

  12. #12
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: WHich one is evaluated first

    Quote Originally Posted by Zaccheus View Post
    Let me get this right ... that does not apply to overloaded && operators then.

    Thinking about how it would be used, with two parameters (or 'this' and one parameter), the right hand parameter would have to be evaluated before the operator could be called.

    I suppose you'd only ever overload it if you did not want the default && behaviour, but it still seems a shame that you cannot keep the 'lazy and' characteristics.

    For smart pointers you could overload the boolean operator instead, but when would you ever want to overload && or || in real code ?
    I highly recommend against the operator bool. If you were to implement it, it means your type could also be cast implicitly to int or double.

    Instead, implement the "operator void const*" and "operator!". void* can be on be implicitly cast implicitly to bool. Since the compiler is only allowed 2 implicit casts before giving up, this make your type convertible to bool, and that is it. This make sure you don't have any weird behaviors.

    std::iostream does it this way.

    In real code, there is no reason to overload && or ||. Even if the overloaded version kept the short-circuit behavior, it is much more practical to allow your type to be evaluated as true or false, and use the default operator.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

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

    Re: WHich one is evaluated first

    Quote Originally Posted by monarch_dodra
    this make your type convertible to bool, and that is it. This make sure you don't have any weird behaviors.
    Not really: weird behaviour is still possible, though less likely.

    Quote Originally Posted by monarch_dodra
    std::iostream does it this way.
    This is weird, to me:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << std::cin;
    }
    The current solution for this weirdness is to implement the safe bool idiom; in the next version of C++ we can simply implement an explicit conversion function to bool.
    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

  14. #14
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: WHich one is evaluated first

    Quote Originally Posted by laserlight View Post
    Not really: weird behaviour is still possible, though less likely.


    This is weird, to me:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << std::cin;
    }
    The current solution for this weirdness is to implement the safe bool idiom; in the next version of C++ we can simply implement an explicit conversion function to bool.
    All correct, but the problem with explicit conversion is that it is not implicit

    Code:
    if (std::cin)
    This would fail to compile with safe conversion. I would say the only absolutely safe way to do it is to provide a "operator bool_proxy", where bool_proxy is a type that can't do anything but be (implicitly) cast to bool.

    Kind of like null_ptr_t* I guess: It's not a pointer, but it can be converted to a pointer.

    *As a matter of fact, null_ptr can be implemented as a template, so I don't see why you can't do the same thing with bool.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

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

    Re: WHich one is evaluated first

    Quote Originally Posted by monarch_dodra
    This would fail to compile with safe conversion.
    No. If you insist otherwise, then be prepared: a standard library implementation that conforms to what is expected to be the next version of the C++ standard will cause your code snippet to fail to compile
    Last edited by laserlight; September 15th, 2010 at 05:06 AM.
    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

Page 1 of 2 12 LastLast

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