Topic: Why STL iterator's post increment and decrement operators return lvalue ???


Author: jpotter@falcon.lhup.edu (John Potter)
Date: 2000/06/27
Raw View
On 20 Jun 2000 19:51:38 GMT, Feng Hou <fenghou@erols.com> wrote:

>
>         I've noticed that STL iterator's post increment and decrement
> operators have a confusing semantics.
>
>     For example,
>
>         list<int> ilist;
>         for (int i=1; i<10; ++i)
>             list.pash_back(i);
>
>         list<int>::iterator beg = list.begin();
>         cout << *beg << endl;          //  output 1
>         beg++++;
>         cout << *beg << end;           // output 2          ???
>
>
>     For a ordinary pointer Ptr, Ptr++++ is a invalid expression since
> post increment returns a rvalue.

Not just that.  For an ordinary pointer, the global pre/post increment
operators require an lvalue.  Note that for an ordinary pointer, ++++Ptr
is valid with undefined behavior.  The prefix operator returns an
lvalue.

> Why iterator's post increment and
> decrement return lvalue (I've looked at SGI STL implementation, all
> iterator class's operator++(int) and operator--(int) return nonconst
> reference) ?

Not in my copy.  They return a temp by value.  It is an rvalue.

> On the other hand, vector and string's iterators are
> implemented using ordinary pointers, other iterators are implemented
> using classes, which causes their post increment and decrement
operators
> have different semantics. Actually, we could let iterator class's
> operator++(int) return a const reference.

To what?

It const& It::operator++ (int) {
   It it(*this);
   ++ *this;
   return it;
   }

A wonderful dangling reference.

> Can anyone explain why SGI STL implementation uses lvalue?

They return an rvalue.  Non-const member functons may be invoked on an
rvalue.  Since it is a member function, iter++++ is valid.  Note that
iter is only incremented once, the second operation is performed on
the temporary returned by the first one.

> How dose C++ standard say about this?

Postfix increment returns a convertable to X const&.  An rvalue is
convertable to X const&.  If you can talk SGI into changing the
postfix operators to non-members, you will almost get what you want.

Iter operator++ (Iter& it, int) {
   Iter tmp(it);
   ++ it;
   return tmp;
   }

Now iter++++ is invalid.  Of course, you can still do ++iter++ unless
you also convince them to make the prefix operator a non-member friend.

Everything works fine.  If it hurts to do iter++++, don't do it.

John



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: Feng Hou <fenghou@erols.com>
Date: 2000/06/20
Raw View
        I've noticed that STL iterator's post increment and decrement
operators have a confusing semantics.

    For example,

        list<int> ilist;
        for (int i=1; i<10; ++i)
            list.pash_back(i);

        list<int>::iterator beg = list.begin();
        cout << *beg << endl;          //  output 1
        beg++++;
        cout << *beg << end;           // output 2          ???


    For a ordinary pointer Ptr, Ptr++++ is a invalid expression since
post increment returns a rvalue.  Why iterator's post increment and
decrement return lvalue (I've looked at SGI STL implementation, all
iterator class's operator++(int) and operator--(int) return nonconst
reference) ? On the other hand, vector and string's iterators are
implemented using ordinary pointers, other iterators are implemented
using classes, which causes their post increment and decrement operators
have different semantics. Actually, we could let iterator class's
operator++(int) return a const reference. Can anyone explain why SGI STL
implementation uses lvalue? How dose C++ standard say about this? Thanks
in advance.

    Regards,
    Jack Hou




      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]