iter_swap on move_iterator

Daniel Krügler
Fri May 18 18:13:00 GMT 2012

2012/5/18 François Dumont <>:
> On 05/17/2012 08:47 PM, Daniel Krügler wrote:
>> 2012/5/17 François Dumont<>:
>>> Hi
>>>    While working on removal of code duplications in algos implementation
>>> I
>>> challenge them with move_iterator. I started with stable_sort and it
>>> doesn't
>>> compile. I summarize the problem to the fact that iter_swap can't be used
>>> on
>>> move_iterator. As, in C++11, iter_swap simply forward to swap(*__a, *__b)
>>> it
>>> is normal that it can't accept rvalue but shouldn't iter_swap be adapted
>>> to
>>> accept move_iterator ?
>> Yes, when the referenced type (or the iterator's reference) provides a
>> swap overload
>> that accepts rvalues. Remember that move_iterator will produce an rvalue
>> on dereference, this rvalue is provided to the swap overload that is found
>> by ADL. Given your description I would expect that the test type does not
>> provide a swap overload accepting rvalues, thus the std::swap is selected,
>> but cannot accept the arguments.
> Ok for how to have iter_swap work with move_iterator, users must grant the
> necessary swap overload. But what about the other algos like stable_sort I
> have tested at first ? Shouldn't they be adapted to work with move_iterator
> ?

I don't think so. The library requirements on the iterator type is
ValueSwappable, which
means that "for any dereferenceable object x of type X, *x is
swappable". So, unless
there exists active rvalue-swap support provided by user-provided value type of
the wrapped iterator of std::move_iterator (or by the reference type
of the wrapped
iterator, once LWG 2106 becomes accepted), the library implementation should
not interfere here.

- Daniel

More information about the Libstdc++ mailing list