Boost logo

Boost :

From: Darin Adler (darin_at_[hidden])
Date: 2000-04-07 12:32:07


on 4/7/00 8:39 AM, Gary Powell at Gary.Powell_at_[hidden] wrote:

> Also is there a consensus on use of std::swap? In the views code we added
> specializations of std::swap(viewType&,viewType&) [...]
>
> namespace std {
> swap(viewType &a,viewType &b)
> {
> a.swap(b);
> }
> }
>
> I've been following a discussion in C.L.C++.M on swap and I'm confused. Is
> it illegal to make more std::swap() specializations? Should my swap
> functions be in my own namespace? Should my swap function do this?
>
> void swap(view &a)
> {
> using std::swap;
> swap(base,a.base);
> }
>
> Will the Koening lookup rules find the correct swap inside my function? Will
> the stand library algorithms use my swap inside their functions?
>
> Any help on this will cause us to fix and adjust.

There's no consensus yet on the whole thing, but there seem to be two
self-consistent views:

A: Use Koenig Lookup:

The standard does prohibit the kind of overload you show above. It's not
"partial specialization of a function template", because that doesn't exist!
All uses of functions from namespace std must use unqualified names to get
full advantage of Koenig Lookup, so you need to do the "using" as above.
Standard library implementations ought to always use unqualified names too;
perhaps the standard should be amended to require this. Names like "swap" in
other namespaces are "reserved"; you must not use them for other purposes
because they might cause trouble for the standard algorithms; perhaps the
standard should be amended to make this clear.

B: Allow Overloading In Namespace std:

Although the standard does prohibit the kind of overload you show above, it
would be better if it didn't; perhaps the standard should be amended to
allow this. All uses of functions from namespace std by the library should
use qualified names, and you need not do the "using" as above. Standard
library implementations ought not use unqualified names either; perhaps the
standard should be amended to require this. (It's not clear what to do about
operators like <<. Should calls to even these be qualified with std:: in
library implementations?)

    -- Darin


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