Implementing extended introspective swap algorithm
<p>I know about ADL and the swap idiom:</p>

<pre><code>using std::swap;
swap(x, y);
</code></pre>

<p><code>boost::swap()</code> does the above for you. Now, I want to push it further. Specifically, Have the swap perform <code>x.swap(y)</code> if possible, and fallback to <code>boost::swap()</code> otherwise. So, you don't have to implement both a member swap and a free one, which is verbose and redundant. I tried to implement such a swap and ended up with the following. Implementing things like this can become quite tricky. So, I'm wondering whether there is any flaws in my implementation, or if more succinct implementations exist.</p>

<pre><code>#include &lt;algorithm&gt;
#include &lt;utility&gt;

namespace cppu_detail_swap {

template &lt;typename T&gt;
void swap_impl(T&amp; x, T&amp; y) {
  using std::swap;
  swap(x, y);
}

} // namespace cppu_detail_swap

namespace cppu {

namespace detail {

template &lt;typename T&gt;
void swap(T&amp; x, T&amp; y, int) {
  cppu_detail_swap::swap_impl(x, y);
}

template &lt;typename T&gt;
auto swap(T&amp; x, T&amp; y, char) -&gt; decltype(x.swap(y)) {
  return x.swap(y);
}

} // namespace detail

template &lt;typename T&gt;
void swap(T&amp; x, T&amp; y) {
  detail::swap(x, y, ' ');
}

} // namespace cppu
</code></pre>