Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2008-03-13 10:14:49


on Thu Mar 13 2008, "Sebastian Theophil" <stheophil-AT-think-cell.com> wrote:

> Dear David,
>
>
> I stumbled upon a problem with the counting_iterator which you seem to
> maintain. If you prefer that I post this message to a boost mailing
> list, let me know and I'll do that.

I do, thanks. Please follow up there.

> The problem in short is that counting_iterator<Value> returns a Value
> const& when it is dereferenced although the counting_iterator doesn't
> point to a sequence of values.

Yep.

> The problem manifests itself already when
> trying to wrap a counting_iterator inside a reverse_iterator.
>
> You might argue that counting_iterators are bidirectional and don't need
> to be reversed.

Sure they do.

> The problem would arise with any application that does
> something like
> Template<class It>
> It::reference_type method(It it) {
> return *(--it);
> }
>
> This returns a reference to the temporary value contained in --it.
>
> I don't see any specific reason why the reference type must be "Value
> const&" and couldn't be e.g "Value". If there is, I'd be happy if you
> could tell me what the reasoning is and what you propose as a
> workaround.

It has to do with the C++03 iterator requirements. If it were not a
reference, then counting_iterator wouldn't be a valid forward iterator
and we'd have to call it an input iterator.

> Thanks a lot and thanks for your great work on boost,
> Sebastian
>
> PS: Of course just changing
>
> typedef iterator_adaptor<
> counting_iterator<Incrementable, CategoryOrTraversal,
> Difference> // self
> , Incrementable //
> Base
> , Incrementable //
> Value
> (...)
> , traversal
> , Incrementable const&
> // reference
> , difference
> > type;
>
> in counting_iterator.hpp
>
> to
>
> typedef iterator_adaptor<
> counting_iterator<Incrementable, CategoryOrTraversal,
> Difference> // self
> , Incrementable //
> Base
> , Incrementable //
> Value
> (...)
> , traversal
> , Incrementable
> // reference
> , difference
> > type;
>
> probably doesn't do the trick. I'd like to work on a real patch unless
> you say it is meant to work that way.

Maybe the trick is to add a parameter to counting_iterator that allows
one to optionally explicitly specify the reference type.

-- 
Dave Abrahams
Boost Consulting
http://boost-consulting.com

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