why does iter_swap internally test for reference being value_type&?

Jonathan Wakely jwakely.gcc@gmail.com
Fri Feb 24 10:37:00 GMT 2012


On 24 February 2012 09:30, Oswin Krause wrote:
> Hi everybody,
>
> I think there is a well thought of reason behind the code, so maybe you
> can help me achieve enlightenment on this issue.
>
> I stumbled across the following piece of code in the implementation of
> iter_swap in bits/stl_algobase.h
>
> std::__iter_swap<
>    __are_same<_ValueType1, _ValueType2>::__value
>  && __are_same<_ValueType1&, _ReferenceType1>::__value
>  && __are_same<_ValueType2&, _ReferenceType2>::__value
>>::iter_swap(__a, __b);
>
> if the meta expression evaluates to true, iter_swap will call swap
> (*__a,*__b), else
> move semantics are used.
>
> The comment of the code pointed me to this older discussion which was
> allready a good read:
> http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html

svn blame shows it was changed by this patch:
http://gcc.gnu.org/viewcvs?view=revision&revision=96837
which references this bug:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20577

Chris does mention the option of overloading swap for vector<bool>:
http://gcc.gnu.org/ml/libstdc++/2005-03/msg00144.html

> Given that the argument is correct, i would advocate the following
> implementation:
>
> std::__iter_swap<
>    __are_same<_ReferenceType1, _ReferenceType2>::__value
>>::iter_swap(__a, __b);

Does that pass the test from the PR?
http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/testsuite/25_algorithms/iter_swap/20577.cc?view=markup
Any proposed change to that code should at least pass the existing tests.



More information about the Libstdc++ mailing list