Boost logo

Boost :

From: Andreas.P.Priesnitz (priesnit_at_[hidden])
Date: 2001-06-05 05:59:37


Hi Jeremy!

Thanks for the counter-example to the operator[] returning a "reference" - I had
missed the according footnote #4 in the "iterator_adaptor" documentation ...
I like the idea, which was also asked for by Vesa, of refining the "random
access" iterator category to distinguish between input/output/both access
provided by operator[], the same way as operator*'s return type is manifested by
the category.
Is there already some public reference to the new iterator categories/concepts
you mentioned?
Ciao!
Andreas

----- Original Message -----
From: "Jeremy Siek" <jsiek_at_r...>
To: <boost_at_[hidden]>
Sent: Friday, June 01, 2001 23:46
Subject: Re: [boost] Re: operator[] for iterator adaptors

> Hi Andreas,
>
> This is a sticky subject :(
>
> In fact, the standard does not require operator[] to return an lvalue,
the
> return type is only required to be convertible to the value_type. Some
> think this is a flaw in the standard, and an issue has been submitted.
>
> Regardless of the standard, there are situations when it is dangerous
for
> the iterator adaptor to return a reference. For example, suppose the
> adapted iterator is reading in elements from a file, and caching each
> object read in as a data member of the iterator, and operator* returns a
> reference to this data member. Now think about what happens inside the
> operator[] of the adaptor: return *(i + n); The addition creates a
> *temporary* iterator, and the dereference returns a reference to a data
> member of this temporary. This means that the reference returned by
> operator[] will be garbage, and lead to catastrophic run-time behaviour.
>
> I really ought to create a version of iterator_adaptor using my new
> iterator categories. I think it would solve this problem.
>
> Cheers,
> Jeremy
>
>
> On Fri, 1 Jun 2001, Andreas.P.Priesnitz wrote:
>
> > Hello!
> > Might somebody give me confirmation/correction, whether I see the following
> > right or not:
> > In "iterator_adaptors.hpp", line 672, the return value of the operator[] is
> > declared to be of type "value_type", which should be "reference" to my
opinion,
> > as operator[], applied to an iterator, is supposed to return an lvalue.
> > The result is obtained from operator*, which - of course - returns a
> > "reference", so that the conversion to "value_type" is really unnecessary -
> > it takes place just in the return statement and can simply be avoided by
> > changing the return type.
> > Or did I miss something?
> > Thankful for comments
> > Andreas Priesnitz


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