Boost logo

Boost Users :

Subject: Re: [Boost-users] [Iterator] swap and iter_swap do not work with zip_iterator
From: Oswin Krause (Oswin.Krause_at_[hidden])
Date: 2012-07-28 02:55:35


Hi,

I don't think that this is intentional. On the other hand: GCCs
iter_swap detects whether the reference_type is an actual reference and
uses a move-implementation when this is not the case(which is a copy
when move is not available). I was a bit flushed about the fact, that
the implementation of iter_swap is different from swap(*a,*b). In this
case it would have been possible to just overload swap for the reference
type of zip_iterator.

Greetings,
Oswin

On 2012-07-28 07:51, Nathan Ridge wrote:
>> zip_iterator can be made to model Swappable Iterator (without
>> changing its reference_type) by overloading iter_swap for
>> zip_iterator as follows:
>>
>> namespace boost
>> {
>> template <typename IteratorTuple>
>> void iter_swap(zip_iterator<IteratorTuple> a,
>> zip_iterator<IteratorTuple> b)
>> {
>> typedef typename zip_iterator<IteratorTuple>::value_type
>> ReferenceTuple;
>> ReferenceTuple ta = *a;
>> ReferenceTuple tb = *b;
>> swap(ta, tb);
>> }
>> }
>>
>> Inserting that snippet above main() in your code, we now get the
>> desired output:
>>
>> 1 2
>> 100 200
>>
>> 2 1
>> 200 100
>>
>> I can't think, off the top of my head, of any reason not to add this
>> overload of iter_swap to Boost.
>
> I just realized that, while this will make your example code work
> correctly, std::sort will still not work because its implementation
> explicitly qualifies calls to iter_swap as std::iter_swap, rather
> than making an unqualified call (which would allow the zip_iterator
> overload to be found by argument-dependent lookup). (At least, GCC
> 4.7's implementation of std::sort does this). Is this intentional?
>
> Regards,
> Nate
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net