Evaluation order for <<

Discussion in 'C++' started by esrrms, Apr 7, 2007.

  1. esrrms

    esrrms New Member

    Joined:
    Apr 7, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Lets say I have a function called html() that returns a string of "<html>". It also saves it state so that the first time it returns "<html>", but the second time it returns "</html>", then on the third it goes back to "<html>";

    If I have this code...
    Code:
    cout<<html();
    cout<<html();
    I get this output...
    Code:
    <html></html>
    But if I have this code...
    Code:
    cout<<html()<<html();
    I get...
    Code:
    </html><html>
    Any way to get the second code to output like the first code?
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Would it be too much trouble to actually post the code for html ()?
     
  3. esrrms

    esrrms New Member

    Joined:
    Apr 7, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    bool b_html;
    
    std::string html()
    {
    std::string s;
    
    if(b_html){
    s = "</html>";
    b_html = false;
    }
    else{
    s = "<html>";
    b_html = true;
    }
    }
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Scratch this post, bad information.
    LATER EDIT
    Consider these statements:
    Code:
    int i = 0;
    cout << i++;
    cout << i++;
    cout << i++ << i++;
    
    This code may produce either 1234 or 1243, depending on the implementation. It is undefined as part of the C++ language. It won't produce 0... anything, because the post-increment operator has higher precedence than the << operator.

    In this code, the only sequence points are represented by the semicolons. A sequence point guarantees that all expressions have been evaluated at that point. The ORDER of evaluation of certain things is defined; the order of evaluation of other things is left up to the implementors of the system. This is based on the fact that the need to do things efficiently may require one method on one implementation and another method on another implementation.

    In the case shown, the << operator has the lowest precedence. It will be evaluated last. Since there are two of them, they WILL be evaluated left-to-right, as you expect.

    The fly in the ointment is that the order of evaluation of the expressions in between the << operators is implementation dependent. It can go either way, just as the order of evaluations of function arguments can. The problem arises when the expressions have side effects. In the last statement, which i++ is evaluated first? Obviously, in your implementation, the second one is evaluated first, then the first one is evaluated, then the first one is output (with the side effects of evaluating the second one in place), then the second one is output.

    Even though the expressions between the << operators may be evaluated in any order, operators WITHIN those expressessions might impose precedence. 2 + 3 * 4, for example, will be evaluated differently than (2 + 3) * 4, but that doesn't impose any necessary order for the encompassing expressions. One has to determine in individual cases if an order is imposed by the language, or if it is undefined and left to the implementation. One then copes. Don't chain them together, in this case. Introduce a sequence point.
     
    Last edited: Apr 8, 2007

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice