Boost logo

Boost :

From: Moore, Paul (paul.moore_at_[hidden])
Date: 2000-04-20 10:17:40

From: Dave Abrahams [mailto:abrahams_at_[hidden]]
> There is no conforming way to extend std::swap for
> user-defined templates.

Is this just true for std::swap? Or does it apply to other functions in
std::? I had similar problems with std::abs (for the rational number class)
which sort-of transmogrified into the swap issue. Steve Cleary just sent me
a copy of a defect report by Alan Griffiths, which describes the problem
using swap(), but which says that the use of swap is just because "the
problem here is particularly acute".

Given that there seems to be a defect in the standard, I'd say Boost needs
to have some sort of view on what acceptable code should do. The options
seem to me to be

1) Accept code which extends std:: functions, as an interim measure. This is
nasty as (a) it prejudges the committee's decision on the defect report, (b)
it introduces undefined behaviour, and (c) someone needs to keep track of
where this is done, so as to fix it when a committee response to the defect
report is available.

2) Require all accepted code to stick to the letter of the standard on this.
This means that boost code must use the existing std::swap, which may not be
efficient (worse, may not be nonthrowing if the class has a throwing copy
constructor??!?) And it offers no real recourse to classes which need to
extend other std:: functions (cf rational and abs).

3) A compromise - require code to put non-conforming code in an #ifdef block
(BOOST_EXTENDS_STD, maybe)? Users can switch this on or not as they please,
and the ifdef documents where the problem exists.

4) Require all code to use something like Alan Griffiths' workaround
        template<typename Y>
        void myswap(Y& lhs, Y& rhs)
            using std::swap;
            swap(lhs, rhs);
which seems to give the desired effect as long as the user also uses the
same workaround. This is the best conforming answer available (AFAIK), but
requires users to implement workarounds in their code - which will probably
remain in maintenance for evermore... Is there any mileage in having a
boost::myswap()? I can't work through the implications of this...

To be honest, I don't like any of the solutions.

Paul Moore.

Boost list run by bdawes at, gregod at, cpdaniel at, john at