Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-09-12 08:30:25


"Peter Dimov" <pdimov_at_[hidden]> writes:

> David Abrahams wrote:
>> FWIW, I think this is a really interesting idea; I just can't quite
>> put the implications together in my mind, so I don't know what to say
>> about it yet.
>
> Quoting from N1296:

Believe it or not, I'm familiar with that paper ;->

The implications of John's proposal differ from what you've outlined
below, though...

> [begin quote:
>
> Only fix std::swap
> ------------------
>
> Although this paper so far discusses the broad problem of standard
> functions in general, many developers actually need to specialize
> one and only one algorithm, namely std::swap. For typical uses of
> the standard library, the performance of std::swap is very
> important. Moreover, we have all been educated on the importance of
> providing a "fast, non-throwing" swap for our classes and that
> swap() is a central technique for building strongly exception-safe
> types out of types which may provide only the basic guarantee. Yet
> the default std::swap is neither fast nor non-throwing. Had the
> standard swap been adequate, this discussion might not even have
> materialized.
>
> There are two ways to improve the default definition of std::swap:
>
> * Make std::swap(a, b) call a.swap(b) for user-defined types and retain the
> current semantics for built-in types.
>
> This would still require users to explicitly provide the fast,
> non-throwing swap

Unlike John's proposal, which falls back to the current swap
semantics when users don't provide swap.

> but will eliminate the need to specialize std::swap for every
> user-defined type. The std::swap overloads for std::vector,
> std::string, etc. may be removed.
>
> * Make the compiler generate std::swap specializations if necessary.
>
> This proposal treats swap as the fundamental operation that many
> believe it is. The rationale for this proposal is that a precedent
> has been already set with the copy constructor and the assignment
> operator. The ability of the compiler to compose new functions by
> recursively applying a function to subobjects can easily be extended
> to std::swap. Such an automatically generated swap() specialization
> will nearly always be adequate (which is not true for the
> automatically generated assignment, for instance.) An explicit user
> specialization will override the compiler-generated version, as
> usual. Unfortunately, it doesn't help the user who wishes to define
> a specialized swap for UDT templates.

As far as I know, defining a specialized swap for UDT templates is
easy with John's proposal, though it is intrusive; you can't do it as
a 3rd party.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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