Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-08-15 09:32:16


"[4] The result type of operator[]() is value_type instead of reference as
might be expected. There are two reasons for this choice. First, the C++
standard only requires that the return type of an arbitrary Random Access
Iterator's operator[]be ``convertible to T'' (Table 76), so when adapting an
arbitrary base iterator we may not have a reference to return. Second, and
more importantly, for certain kinds of iterators, returning a reference
could cause serious memory problems due to the reference being bound to a
temporary object whose lifetime ends inside of the operator[]."

First: the operational semantics of a[n] are *(a + n), and that returns a
reference.
Second: could you please show an example?

----- Original Message -----
From: "David Abrahams" <dave_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, August 15, 2002 17:02
Subject: Re: [boost] Iterator adaptors return a value from operator[]?

> http://www.boost.org/libs/utility/iterator_adaptors.htm#4
>
> -----------------------------------------------------------
> David Abrahams * Boost Consulting
> dave_at_[hidden] * http://www.boost-consulting.com
>
>
> ----- Original Message -----
> From: "Peter Dimov" <pdimov_at_[hidden]>
> To: "Boost List" <boost_at_[hidden]>
> Sent: Thursday, August 15, 2002 8:50 AM
> Subject: [boost] Iterator adaptors return a value from operator[]?
>
>
> > Why? (Yes I know that a[n] is convertible to T, but it's also equivalent
> to
> > *(a + n), which is T&; a[n] not being T& is a defect IMO.)
> >
> > --
> > Peter Dimov
> > http://www.mmltd.net/pdimov
> >
> > _______________________________________________
> > Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>
> _______________________________________________
> Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk