Boost logo

Boost :

Subject: Re: [boost] [Iterator] Zip iterator for back_insert_iterators?
From: David Abrahams (dave_at_[hidden])
Date: 2008-09-09 16:44:37


on Tue Sep 09 2008, "Kris Rousey" <krousey-AT-gmail.com> wrote:

> 2008/9/9 David Abrahams <dave_at_[hidden]>:
>
>> Try this patch to boost/iterator/iterator_facade.hpp; does it fix the
>> problem for you?
>
> Yes. Well... It fixed THAT problem. After I took it a step further and
> tried to use the iterator like this:
>
> *iter = make_tuple(i, i*2.0);
>
> I then get this error:
>
> boost/tuple/detail/tuple_basic.hpp:377: error: no match for
> 'operator=' in '((boost::tuples::cons<void, boost::tuples::cons<void,
> boost::tuples::null_type> >*)this)->boost::tuples::cons<void,
> boost::tuples::cons<void, boost::tuples::null_type> >::head =
> u->boost::tuples::cons<int, boost::tuples::cons<double,
> boost::tuples::null_type> >::head'
>
> After looking into it further, it seems that std::back_insert_iterator
> inherits from std::iterator<std::output_iterator_tag, void, void,
> void, void>. Since it obviously has no interest in populating the
> right type information, would it make sense add partial
> specializations to boost/detail/iterator.hpp to grab the appropriate
> types from the container? e.g.
>
> template <class CONTAINER>
> struct iterator_traits<std::back_insert_iterator<CONTAINER> >
> {
> typedef typename CONTAINER::value_type value_type;
> typedef typename CONTAINER::reference reference;
> typedef typename CONTAINER::pointer pointer;
> typedef typename CONTAINER::difference_type difference_type;
> typedef std::output_iterator_tag iterator_category;
> };

Well, that's a very clever idea. It subverts the purpose of
boost/detail/iterator.hpp which was intended to be not much more than a
wrapper for std::iterator_traits to work around compiler/library
implementation deficiencies, but something like that just might work.
That said, the reference type is definitely wrong in your
specialization; it should be the same as the type returned by
dereferencing the back_insert_iterator (I think that might be the
back_insert_iterator type itself, IIRC).

> I'll try this out and see if it works, but what is this the right way
> to go about it?
>
> I'm fairly new to C++ and boost development, so I'm
> looking to learn the correct ways.

Well, the *really* right way to go about it is to write a fix that's not
specific to back_insert_iterator, but that works for all kinds of output
iterators that are not also forward iterators. That would take a little
thought to get right, but it would be a good project for you to take on.

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

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