Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-06-01 15:46:53


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
>
>
> To unsubscribe, send email to: <mailto:boost-unsubscribe_at_[hidden]>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>

----------------------------------------------------------------------
 Jeremy Siek www: http://www.lsc.nd.edu/~jsiek/
 Ph.D. Candidate, IU B'ton email: jsiek_at_[hidden]
 Summer Manager, AT&T Research phone: (973) 360-8185
----------------------------------------------------------------------


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