François Dumont | 17 May 2012 19:53
Picon

iter_swap on move_iterator

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 ?

     I also find the compilation error message surprising. I have added 
a call to iter_swap in 24_iterators/move_iterator/greedy_ops.cc and the 
error message is:

In file included from

/home/fdt/dev/gcc-build/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/char_traits.h:41:0, 

                  from 
/home/fdt/dev/gcc-build/x86_64-unknown-linux-gnu/libstdc++-v3/include/ios:41, 

                  from 
/home/fdt/dev/gcc-build/x86_64-unknown-linux-gnu/libstdc++-v3/include/ostream:40, 

                  from 
/home/fdt/dev/gcc-build/x86_64-unknown-linux-gnu/libstdc++-v3/include/iterator:64, 

                  from 
/home/fdt/dev/gcc/libstdc++-v3/testsuite/24_iterators/move_iterator/greedy_ops.cc:20: 

/home/fdt/dev/gcc-build/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/stl_algobase.h: 
(Continue reading)

Daniel Krügler | 17 May 2012 20:47

Re: iter_swap on move_iterator

2012/5/17 François Dumont <frs.dumont <at> gmail.com>:
> 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.

> Why is the comipiler talking about a call to swap on
> move_iterator<greedy_ops::X*>::value_type. It should be on the return type
> of move_iterator operator* which is move_iterator<>::reference, no ?

Yes, but move_iterator<>::reference is value_type&&.

- Daniel

François Dumont | 18 May 2012 14:21
Picon

Re: iter_swap on move_iterator

On 05/17/2012 08:47 PM, Daniel Krügler wrote:
> 2012/5/17 François Dumont<frs.dumont <at> gmail.com>:
>> 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 ?
>
>> Why is the comipiler talking about a call to swap on
>> move_iterator<greedy_ops::X*>::value_type. It should be on the return type
>> of move_iterator operator* which is move_iterator<>::reference, no ?
> Yes, but move_iterator<>::reference is value_type&&.
Agree but the compiler message do not show the '&&', this is especially 
obvious in:

/home/fdt/dev/gcc-build/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/move.h:166:5: 
(Continue reading)

Daniel Krügler | 18 May 2012 20:12

Re: iter_swap on move_iterator

2012/5/18 François Dumont <frs.dumont <at> gmail.com>:
> On 05/17/2012 08:47 PM, Daniel Krügler wrote:
>>
>> 2012/5/17 François Dumont<frs.dumont <at> gmail.com>:
>>>
>>> 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
(Continue reading)

Jonathan Wakely | 19 May 2012 02:07
Picon

Re: iter_swap on move_iterator

On 18 May 2012 19:12, Daniel Krügler wrote:
> 2012/5/18 François Dumont <frs.dumont <at> gmail.com>:
>> On 05/17/2012 08:47 PM, Daniel Krügler wrote:
>>>
>>> 2012/5/17 François Dumont<frs.dumont <at> gmail.com>:
>>>>
>>>> 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
(Continue reading)

Christopher Jefferson | 19 May 2012 10:01
Picon
Favicon

Re: iter_swap on move_iterator

On 19/05/12 01:07, Jonathan Wakely wrote:
> On 18 May 2012 19:12, Daniel Krügler wrote:
>> 2012/5/18 François Dumont<frs.dumont <at> gmail.com>:
>>> On 05/17/2012 08:47 PM, Daniel Krügler wrote:
>>>> 2012/5/17 François Dumont<frs.dumont <at> gmail.com>:
>>>>> 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
(Continue reading)


Gmane