Boost logo

Boost Users :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-09-26 09:49:12


On Thursday 26 September 2002 10:30 am, Galante Michele wrote:
> Why is the global swap() function defined in namespace boost and not as a
> specialization of std::swap<>() in namespace std?

Most Boost classes are templates, so we cannot define a specialization of
std::swap<>() for them, and we aren't allowed to add an overload into std::.

> Suppose I have a template function like the following:
>
> #include <algorithms>
>
> template <typename T>
> void do_something_and_swap(T &a, T &b)
> {
> // do something ...
> std::swap(a, b);
> }
>
> I want this template function to work with every type that is "swappable",
> but if I try to use it with boost::scoped_ptr<> the compiler fails because
> std::swap<>() is not specilized for boost::scoped_ptr<> and the generic
> definition of std::swap<>() requires the argument type to be copyable (and
> boost::scoped_ptr<> is noncopyable).

You want:

template <typename T>
void do_something_and_swap(T &a, T &b)
{
  // do something ...
  using std::swap;
  swap(a, b);
}

Argument-dependent lookup will ensure that any 'swap' functions in the
namespaces used by 'T' will be included in the set of known swap functions,
along with std::swap. Overloading and partial ordering of function templates
will then take over to ensure that the appropriate swap(a, b) will be called
(even if it is boost::swap).

        Doug


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net