我正在尝试使类模板可交换。我不明白为什么静态断言会失败:
#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 (){}
它通过
template <class T>
constexpr void
swap (A<T>&, A<T>&) {}
但是第一个不应该也可以吗?
最佳答案
std::is_swappable
(和 std::is_swappable_with
)也考虑了 std::swap
。
... are both well-formed in unevaluated context after using
std::swap
;
那么对于第一种情况,swap
的调用在用户定义的swap
和std::swap
之间是不明确的.对于第二个,用户定义的 swap
在重载决议中获胜,然后 std::is_swappable
按预期工作。
关于c++ - 创建一个类模板 std::is_swappable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68844718/