
Would anyone be interested in me adding an koenig lookup swap function to boost? (I would say it belongs in boost.utility). It would be a swap function that uses Koenig lookup to find the 'best' swap funciton, falling back on std::swap like this: template <class T> void koenig_swap(T& left, T& right) { using std::swap; swap(left,right); } Several boost libraries (multi array, compressed pair, graph, optional, spirit) already use this technique, each time explicitly with 'using std::swap' instead of abstracting it out.

on Sat Aug 11 2007, "Joseph Gauterin" <joseph.gauterin-AT-googlemail.com> wrote:
Would anyone be interested in me adding an koenig lookup swap function to boost? (I would say it belongs in boost.utility).
It would be a swap function that uses Koenig lookup to find the 'best' swap funciton, falling back on std::swap like this:
template <class T> void koenig_swap(T& left, T& right) { using std::swap; swap(left,right); }
Several boost libraries (multi array, compressed pair, graph, optional, spirit) already use this technique, each time explicitly with 'using std::swap' instead of abstracting it out.
The appropriate name for that function is boost::swap :-) -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com

AMDG David Abrahams <dave <at> boost-consulting.com> writes:
template <class T> void koenig_swap(T& left, T& right) { using std::swap; swap(left,right); }
Several boost libraries (multi array, compressed pair, graph, optional, spirit) already use this technique, each time explicitly with 'using std::swap' instead of abstracting it out.
The appropriate name for that function is boost::swap
To prevent infinite recursion or ambiguity we need: namespace boost_swap_impl { template<class T> void swap_impl(T& left, T&, right) { using std::swap; swap(left, right); } } namespace boost { namespace swap_adl_barrier { template<class T> void swap(T& left, T& right) { ::boost_swap_impl::swap_impl(left, right); } } using swap_adl_barrier::swap; } In Christ, Steven Watanabe

on Sat Aug 11 2007, Steven Watanabe <steven-AT-providere-consulting.com> wrote:
To prevent infinite recursion or ambiguity we need:
namespace boost_swap_impl {
template<class T> void swap_impl(T& left, T&, right) { using std::swap; swap(left, right); }
}
namespace boost {
namespace swap_adl_barrier {
template<class T> void swap(T& left, T& right) { ::boost_swap_impl::swap_impl(left, right); }
}
using swap_adl_barrier::swap;
}
Yep -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com

On Sat, 11 Aug 2007, Joseph Gauterin wrote:
Would anyone be interested in me adding an koenig lookup swap function to boost? (I would say it belongs in boost.utility).
It would be a swap function that uses Koenig lookup to find the 'best' swap funciton, falling back on std::swap like this:
template <class T> void koenig_swap(T& left, T& right) { using std::swap; swap(left,right); }
Several boost libraries (multi array, compressed pair, graph, optional, spirit) already use this technique, each time explicitly with 'using std::swap' instead of abstracting it out.
I feel a little dumb, but I have to ask. What is the problem with overloading std::swap? What does this koenig_swap offer better? -- François Duranleau

Although it's legal to overload std::swap for custom types, in my experience it's very uncommon to do so. More importantly than my personal experience though - no boost classes provide a swap function in namespace std.
From a conceptual point of view, a non-member swap function forms part of a classes public interface, so it really belongs in the same namespace.
participants (4)
-
David Abrahams
-
François Duranleau
-
Joseph Gauterin
-
Steven Watanabe