template<class T> void invoke_swap(T& left, T& right)
The template function
allows the values of two variables to be swapped, using argument dependent
lookup to select a specialized swap function if available. If no specialized
swap function is available,
std::swap function requires that the elements
to be swapped are assignable and copy constructible. It is usually implemented
using one copy construction and two assignments (C++11 replaces copy operations
with move) - this is often both unnecessarily restrictive and unnecessarily
slow. In addition, where the generic swap implementation provides only the
basic guarantee, specialized swap functions are often able to provide the
no-throw exception guarantee (and it is considered best practice to do so
The alternative to using argument dependent lookup in this situation is to
provide a template specialization of
for every type that requires a specialized swap. Although this is legal C++,
no Boost libraries use this method, whereas many Boost libraries provide
specialized swap functions in their own namespaces.
boost::core::invoke_swap also supports swapping built-in
arrays. Note that
std::swap originally did not do so, but a request
to add an overload of
for built-in arrays has been accepted by the C++ Standards Committee.
boost::core::invoke_swap provides the same exception
guarantee as the underlying swap function used, with one exception; for an
array of type
1 and the underlying swap function
T provides the strong
provides only the basic exception guarantee.
In C++11 and later,
propagates the same
specification as the one specified in the underlying swap function.
Tmust be copy assignable (since C++11: move assignable)
Tmust be copy constructible (since C++11: move constructible)
swap(T&, T&)is available via argument dependent lookup
Tis a built-in array of swappable elements
Several older compilers do not support argument dependent lookup. On these
boost::core::invoke_swap will call
ignoring any specialized swap functions that could be found as a result of
argument dependent lookup.
boost::swapless specialized than
std::swap, thereby allowing the function to have the name 'swap' without introducing ambiguity. However, later the function was renamed to
boost::core::invoke_swapto avoid potential infinite recursion.
 Scott Meyers, Effective C++ Third Edition, Item 25: "Consider support for a non-throwing swap"