I would consider "proper" implementation for is_swappable
to be the following:
template<class T, class U = T> struct is_swappable<T, U> : /* see below */ { }
is_swappable
inherits from std::true_type
if T and U are Swappable
, otherwise from std::false_type
.
I have tried many things, but SFINAE just doesn't seem to work. This is a particularly nasty counterexample:
struct A {
A() {}
~A() {}
A(const A&) = delete;
A(A&&) = delete;
};
Clearly A
is not Swappable
. Yet any generic solution I can come up with does not properly handle the above example.
A SFINAE implementation I have tried, but did not work looked like this:
namespace with_std_swap {
using std::swap;
template<class T, class U, class =
decltype(swap(std::declval<T&>(), std::declval<U&>()))>
std::true_type swappable_test(int);
template<class, class> std::false_type swappable_test(...);
}
template<class T, class U = T>
struct is_swappable
: decltype(with_std_swap::using_std_swap::swappable_test<T, U>(0)) { };
Is there any way to code is_swappable
without compiler help?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…