CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    Join Date
    Oct 2008
    Posts
    1,456

    Re: WHich one is evaluated first

    Quote Originally Posted by laserlight View Post
    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
    Just to be more specific ( and to see if I got it right ), in the next standard implicit boolean conversions are treated as direct initializations of the boolean target ( that is "bool b(t);" and not "bool b = t;" ). For this reason that code * would compile with an explicit bool conversion function (laserlight, correct me if I'm wrong ).

    *: I mean the "if( std::cin )" snippet

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

    Re: WHich one is evaluated first

    Quote Originally Posted by laserlight View Post
    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
    Holy crap, you're right!

    Code:
    27.5.4 Class template basic_ios [ios]
    namespace std {
    template <class charT, class traits = char_traits<charT> >
    class basic_ios : public ios_base {
    public:
    // types:
    typedef charT char_type;
    typedef typename traits::int_type int_type;
    typedef typename traits::pos_type pos_type;
    typedef typename traits::off_type off_type;
    typedef traits traits_type;
    explicit operator bool() const;
    bool operator!() const;
    iostate rdstate() const;
    void clear(iostate state = goodbit);
    void setstate(iostate state);
    bool good() const;
    bool eof() const;
    bool fail() const;
    bool bad() const;
    That's gonna break a lot of code!

    Guess I'll just use

    Code:
    while (!!std::cin)
    Much faster than typing static_cast<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.

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

    Re: WHich one is evaluated first

    From the Wikipedia page of C++0x:

    In C++0x, the explicit keyword can now be applied to conversion operators. As with constructors, it prevents the use of those conversion functions in implicit conversions. However, language contexts that specifically require a boolean value (the conditions of if-statements and loops, as well as operands to the logical operators) count as explicit conversions and can thus use a bool conversion operator.
    So it's not that bad.

    And as far as I'm aware, the C++0x committee goes out of its way not to break existing code.
    Old Unix programmers never die, they just mv to /dev/null

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

    Re: WHich one is evaluated first

    Quote Originally Posted by HighCommander4 View Post
    From the Wikipedia page of C++0x:



    So it's not that bad.

    And as far as I'm aware, the C++0x committee goes out of its way not to break existing code.
    I see. But what about things like

    Code:
    while( (std::cin >> a) && a!=0 )
    ?
    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.

  5. #20
    Join Date
    Jun 2008
    Posts
    592

    Re: WHich one is evaluated first

    Quote Originally Posted by monarch_dodra
    Much faster than typing static_cast<bool>
    but what about (bool) ? and !! isn't that informative on what that actually means
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

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

    Re: WHich one is evaluated first

    Quote Originally Posted by monarch_dodra
    I see.
    Err... I was being serious. As far as I know, explicit conversion functions to bool are specifically designed to replace the safe bool idiom and other existing methods to try and convert to bool without allowing unexpected behaviour, i.e., it is the case that it is not going to break existing code.

    Quote Originally Posted by monarch_dodra
    But what about things like
    Code:
    while( (std::cin >> a) && a!=0 )
    Consider:
    Quote Originally Posted by C++ Standard Working Draft 2010-08-21 Clause 4 Paragraph 3 (part)
    An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t. Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t. The effect of either implicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion.
    Quote Originally Posted by C++ Standard Working Draft 2010-08-21 Clause 5.14 Paragraph 1 (part)
    The && operator groups left-to-right. The operands are both contextually converted to type bool (Clause 4).
    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

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

    Re: WHich one is evaluated first

    Quote Originally Posted by Joeman View Post
    but what about (bool) ? and !! isn't that informative on what that actually means
    well, bool is a C-cast. I stay clear away from those. !! is just as informative as static_cast<bool> if you're used to them. As always, it is just a language construct you have seen before and understand, or it isn't and you have to think about it. Were I work, we have objects that can't be cast to bool, but have operator!. We use "if (!!a)" all the time, and when we see "!!" we know exactly what it means. And it's safe.

    Quote Originally Posted by laserlight View Post
    Err... I was being serious. As far as I know, explicit conversion functions to bool are specifically designed to replace the safe bool idiom and other existing methods to try and convert to bool without allowing unexpected behaviour, i.e., it is the case that it is not going to break existing code.
    I was acknowledging what was being said to me. Maybe "I see" can be interpreted as sarcasm in some context, but this was not the case here.

    Thank you very much. You have taught me two very interesting things today. Pardon me if my surprised looked like mockery. It wasn't.
    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.

  8. #23
    Join Date
    Jun 2008
    Posts
    592

    Re: WHich one is evaluated first

    Quote Originally Posted by monarch_dodra
    well, bool is a C-cast. I stay clear away from those
    Yes, static_cast is preferable, but when it comes to the primitive types, the c-cast works fine. I only worry when it comes to polymorphism and class pointers in general.

    I would hate to write this all over my code
    Code:
    int I = 10;
    int I2 = 3;
    float F = static_cast<float>( I ) /  I2;
    vs
    Code:
    int I = 10;
    int I2 = 3;
    float F = (float) I  / I2;
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

Page 2 of 2 FirstFirst 12

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