RE: [Boost-Users] std::swap() vs boost::swap()
From: Galante Michele [mailto:M.Galante@centrosistemi.it]
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
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. 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
participants (1)
-
sclearyï¼ jerviswebb.com