Boost logo

Boost :

From: Paul Moore (gustav_at_[hidden])
Date: 2001-01-23 17:29:42


On 23 Jan 2001, at 14:07, Daryle Walker wrote:

> on 1/21/01 10:33 PM, Paul Moore at gustav_at_[hidden] wrote:
>
> [SNIP]
> > This could be replaced with
> >
> > IntType r(n%m);
> > std::swap(n,m)
> > std::swap(m,r);
>
> Does it have to be the "swap" from the "std" namespace? Could it be
> generalized to just "swap" like
>
> //=============================
> using std::swap;
> IntType r( n % m );
> swap( n, m );
> swap( m, r );
> //=============================

Blast, damn, @*&%$! and other swear words. You're probably
right... I thought for some reason that swap was one of the ones
you could insert into std::. I'm not sure why I thought this.

This brings up all the nasty issues I had with abs(), on which
subject I just posted a message explaining why I have decided to
abandon my attempts to get rational's abs() to use a user-supplied
abs().

At least with swap() there is a fallback which applies to all types in
std::. So we don't have to worry about a built-in type without a
swap() implementation. But we still have the worry of compilers
which don't do Koenig lookup, the gcc bug where using statements
at function scope are ignored, and so on.

I'll probably not bother with the swap() optimisation then. Maybe I'll
put a comment in the code explaining the optimisation, plus a
reminder to re-enable it once gcc and MSVC support Koenig
lookup.

Paul.

PS In case people think I'm just being petluant, I should point out
that without Koenig lookup, the swap() and abs() stuff simply
doesn't work. With abs(), you get compile errors, and with swap()
you get the wrong version being used (resulting in at best no
improvement, and at worst a severe pessimization of the code). I'd
rather leave it out than have broken code. And as I've said before, I
have to use MSVC, so I won't implement something which doesn't
work on that compiler.

PPS Is it only me who feels that the "using std::swap" incantation
required to be able to use swap() unqualified and hence to get the
benefit of Koenig lookup is clumsy, unintuitive and inelegant?


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