我正在尝试使类模板可交换。我不明白为什么静态断言失败:
#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>&) {}
但第一个不应该也可以吗?
神箭:https://godbolt.org/z/vbWWxrec4 https://godbolt.org/z/vbWWxrec4
std::is_swappable https://en.cppreference.com/w/cpp/types/is_swappable (and std::is_swappable_with
) 认为std::swap
too.
...使用后在未评估的上下文中都是格式良好的std::swap
;
然后对于第一种情况,调用swap
用户定义之间不明确swap
and std::swap https://en.cppreference.com/w/cpp/algorithm/swap。对于第二个,用户定义swap
然后在重载决策中获胜std::is_swappable
按预期工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)