Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-09-11 10:13:43


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:

[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, 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.

(Note: It may be worth discussing whether the signature of std::swap should
change to

  template<class T> void swap(T &, T &) throw();

in both cases.)

--end quote.]


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