Streaming and order of precedence

Discussion in 'C++' started by d.stonier@gmail.com, Mar 19, 2008.

  1. Guest

    Hi all,

    I've recently had some issues with the streaming operator and the
    order of precedence used when mixing it with the member function
    operator (operator .) Example code is below.

    *************************************************************************
    #include <iostream>
    #include <vector>
    using namespace std;

    class A
    {
    public:
    A() : i(0) {};
    int f() {
    i++;
    v.push_back(i);
    return i;
    }

    void reset() { i = 0; v.clear(); }

    int i;
    vector<int> v;
    };

    int main()
    {
    A a;

    // Order of action:
    // - Member functions fire from RIGHT to LEFT.
    // - Insertion operators fire from LEFT to RIGHT
    cout << a.f() << a.f();
    cout << endl;

    cout << "Vector contents a: ";
    for (unsigned int i = 0; i < a.v.size(); ++i )
    {
    cout << a.v.at(i);
    }
    cout << endl;

    a.reset();
    int k;
    // Order of action;
    // - Member functions fire from LEFT to RIGHT
    // - Addition operator fires.
    // - Assignment operator fires.
    k = a.f() + 3*a.f();
    cout << "k: " << k << endl;
    cout << "Vector contents a: ";
    for (unsigned int i = 0; i < a.v.size(); ++i )
    {
    cout << a.v.at(i);
    }
    return 0;
    }
    *********************************************************************************
    OUTPUT:
    21
    Vector contents a: 12
    k: 7
    Vector contents a: 12
    *********************************************************************************

    The result of this seems to indicate that the . operator for the
    member function call has different associativities in the different
    situations. I must be missing something here, but I haven't been able
    to find anyone to give me a conclusive answer on it. Can anyone here
    shed some light on it?

    Cheers,
    Daniel.
     
    , Mar 19, 2008
    #1
    1. Advertising

  2. White Wolf Guest

    > wrote:
    [SNIP]
    > OUTPUT:
    > 21
    > Vector contents a: 12
    > k: 7
    > Vector contents a: 12


    Well, this output seems fine to me. What did you expect to see?

    ww (Attila)
     
    White Wolf, Mar 19, 2008
    #2
    1. Advertising

  3. Snorri Guest

    On Mar 19, 6:20 pm, "White Wolf" <> wrote:
    > > wrote:
    >
    > [SNIP]
    >
    > > OUTPUT:
    > > 21
    > > Vector contents a: 12
    > > k: 7
    > > Vector contents a: 12

    >
    > Well, this output seems fine to me.  What did you expect to see?
    >
    > ww (Attila)


    Expected a '12' on the first line in the cout. Curious as to why the
    multiple a.f() calls are worked out from right to left in the
    streaming case, but from left to right when doing the arithmetic case.
    Is precedence and associativity for the '.' operator not entirely
    fixed?
     
    Snorri, Mar 19, 2008
    #3
  4. Paavo Helde Guest

    Snorri <> wrote in news:8230ccf5-b873-4478-8840-
    :

    > On Mar 19, 6:20 pm, "White Wolf" <> wrote:
    >> > wrote:
    >>
    >> [SNIP]
    >>
    >> > OUTPUT:
    >> > 21
    >> > Vector contents a: 12
    >> > k: 7
    >> > Vector contents a: 12

    >>
    >> Well, this output seems fine to me.  What did you expect to see?
    >>
    >> ww (Attila)

    >
    > Expected a '12' on the first line in the cout. Curious as to why the
    > multiple a.f() calls are worked out from right to left in the
    > streaming case, but from left to right when doing the arithmetic case.
    > Is precedence and associativity for the '.' operator not entirely
    > fixed?


    The order of evaluation in this case is left unspecified (I guess in
    order to give compilers a chance to optimize code better, or by historic
    reasons).

    Here using i++ twice during a full expression gives you merely
    unspecified behavior because of sequence points at function enter/exit.
    Without functions the behaviour would be undefined - see
    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-
    39.15

    hth
    Paavo
     
    Paavo Helde, Mar 19, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    26
    Views:
    678
    Mark McIntyre
    Jun 15, 2007
  2. vjay
    Replies:
    7
    Views:
    386
    Robin Kåveland Hansen
    Nov 27, 2007
  3. Snorri
    Replies:
    1
    Views:
    291
    Victor Bazarov
    Mar 19, 2008
  4. karthikbalaguru
    Replies:
    4
    Views:
    379
    Peter Nilsson
    Sep 21, 2009
  5. Meske Moday

    Order of precedence and associativity

    Meske Moday, Jun 15, 2010, in forum: C Programming
    Replies:
    22
    Views:
    730
    David Thompson
    Jul 2, 2010
Loading...

Share This Page