le migou
le migou

Reputation: 471

Make a class template std::is_swappable

I'm trying to make a class template swappable. I don't understand why the static assertion fails:

#include <type_traits>

template <class T>
struct A {};

template <class T, class U>
constexpr void
swap (A <T>&, A <U>&) {}

static_assert (std::is_swappable_v <A <int>>);

int main (){}

It passes with

template <class T>
constexpr void
swap (A<T>&, A<T>&) {}

but shouldn't the first one also be OK?

godbolt: https://godbolt.org/z/vbWWxrec4

Upvotes: 3

Views: 134

Answers (1)

songyuanyao
songyuanyao

Reputation: 172934

std::is_swappable (and std::is_swappable_with) considers std::swap too.

... are both well-formed in unevaluated context after using std::swap;

Then for the 1st case, the calling of swap is ambiguous between user-defined swap and std::swap. For the 2nd one, user-defined swap wins in overload resolution then std::is_swappable works as expected.

Upvotes: 3

Related Questions