Boost logo

Boost Users :

From: scleary_at_[hidden]
Date: 2002-09-26 09:47:35


> From: Galante Michele [mailto:M.Galante_at_[hidden]]
>
> 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).

The best way to provide this solution is to use an unqualified swap; Koenig
lookup will ensure that the correct swap() is found for std:: and boost::
components.

> I think I have read somewhere (but perhaps I'm wrong) that specialization
of
> std::swap<>() for user defined types is allowed by the standard, so is
there
> any other reason for not defining the global swap functions in namespace
> std?

Technically, specialization of std::swap is allowed. But overloading
std::swap is not. These are often confused, but here's a concrete example:
  template <>
  void std::swap(boost::scoped_ptr<int> &, boost::scoped_ptr<int> &)
is a specialization, and legal; but:
  template <typename T>
  void std::swap(boost::scoped_ptr<T> &, boost::scoped_ptr<T> &)
is an overload, and not legal (currently).

There's been a ton of discussion of this issue on the Boost.Developer's
list; you can search the list archives if you're interested.

        -Steve


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