Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-08-24 10:05:50


On Fri, 24 Aug 2001, David Abrahams wrote:
david.> >
david.> > is there a reason (I guess, so: which?) why iterator_adaptor
david.> > just provides const operator[], i.e.
david.> >
david.> > value_type operator[](difference_type n) const
david.> > { return *(*this + n); }
david.> >
david.> > but not the non-const version, something like
david.> >
david.> > reference operator[](difference_type n)
david.> > { return *(*this + n); }
david.> >
david.> > Thanks,
david.> >
david.> > - Roland
david.>
david.> See footnote 4 in the documentation.

Actually, footnote 4 explains why the return type is value_type and not
reference, but it does not explain why there is only one operator[] and
not two. The reason is that it is incorrect to have a non-const operator[]
on an iterator. This is a common misconception, because it differs from
containers, which should have both const and non-const operator[].
Dereferencing an iterator does not change the iterator object (unlike
incrementing the iterator) and therefore dereferencing (and also
operator[]) should be const methods. Also, a particular iterator type is
either mutable or constant, but not both, and the difference between the
mutable and constant variaties is that for mutable, the reference type is
a non-const reference and for the constant variaties the reference type is
const. Remember that the mutable or constantness of the iterator refers to
the objects pointed to, not the iterator object itself. Whether the
iterator object itself is "const" or not has nothing to do with whether
the iterator is mutable or constant.

david.> > PS: I just saw that in the documentation some functions of
david.> > default_iterator_policies are still declared static,
david.> > whereas in the actual code they aren't.
david.>
david.> Fixed in CVS, thanks. Also, there were a few 'const's missing.
david.>
david.> -Dave
david.>
david.>
david.> Info: http://www.boost.org Unsubscribe: <mailto:boost-unsubscribe_at_[hidden]>
david.>
david.> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
david.>
david.>
david.>

----------------------------------------------------------------------
 Jeremy Siek http://php.indiana.edu/~jsiek/
 Ph.D. Student, Indiana Univ. B'ton email: jsiek_at_[hidden]
 C++ Booster (http://www.boost.org) office phone: (812) 855-9761
----------------------------------------------------------------------


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