Boost logo

Boost Users :

From: Galante Michele (M.Galante_at_[hidden])
Date: 2002-09-26 09:30:12


Boost classes that define a swap() member function usually also provide a
global swap() function:

  namespace boost
  {
    class X
    {
      void swap(X &rhs);
    };
    template<class T> void swap(X &lhs, X &rhs)
    {
      lhs.swap(rhs);
    }
  }

Why is the global swap() function defined in namespace boost and not as a
specialization of std::swap<>() in namespace 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).

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?

Michele Galante
Zucchetti Centro Sistemi s.p.a.
m.galante_at_[hidden]


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