Boost logo

Boost :

Subject: Re: [boost] [tuple] swap (also includes question about [sorting])
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-05-17 01:13:04


AMDG

Jeremiah Willcock wrote:
> Thanks to Marcin Zalewski, the problem in my last email has been
> tracked down to the inability to swap boost::tuples that contain
> references. There have been a couple of proposals to add that:
>
> http://lists.boost.org/Archives/boost/2006/03/102603.php
> http://lists.boost.org/Archives/boost/2003/05/48488.php
>
> but it appears that none of them has made it into Boost. This feature
> is important because of sorting data structures using zip_iterators as
> in my previous message:
>
> http://www.nabble.com/-iterator--std::sort-on-zip_iterator-td23348384.html
>
>
> and the older posts:
>
> http://lists.boost.org/Archives/boost/2004/07/68758.php
> http://lists.boost.org/boost-users/2009/03/46623.php
>
> In particular, std::swap (from GCC 4.0.1) does not work correctly on
> the result of dereferencing a zip_iterator, which is a Boost tuple of
> references. The std::swap function does not work at all (fails at
> compile time) because the result of operator* is not a non-const
> reference, and std::iter_swap tries to use assignment between the
> tuples, producing incorrect answers at run time. Is it true that
> there is no standards-compliant way to solve this problem? If so,
> would that be a good justification for Boost.Sorting to include an
> algorithm similar to std::sort but that handles more generalized
> swappable types?

Even overloading swap is insufficient, since the standard does not
specify that
std::sort calls swap using ADL or even that it calls any swap at all.
In particular
overloading swap will not work on msvc, because std::sort calls
std::iter_swap
(without ADL), which in turn calls std::swap.

In Christ,
Steven Watanabe


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