根据非成员swap()实施成员swap()

问题描述 投票:2回答:1

我正在实现一个具有与std::array相似的接口的类,该类同时具有member swap()swap()

由于我想让我的班级模仿标准容器,所以我想实现两种non-member swap()(非成员swap()是通过ADL实现的,因为不允许专门使用swap():]]]

swap()

但是,似乎我无法从类内部调用非成员std::swap(),因为即使它只有一个参数,它也更喜欢成员class A { public: friend void swap(A& a, A& b) { /* swap the stuff */ } void swap(A& other) { swap(*this, other); } }; 。将其更改为swap()也不起作用,因为只能通过ADL找到类中的好友函数。我怎么能从班级内部调用非成员swap()

我正在实现一个具有与std :: array类似的接口的类,该类同时具有成员swap()和非成员swap()。因为我希望我的班级模仿标准容器,所以我想...

c++ c++17 swap overload-resolution argument-dependent-lookup
1个回答
0
投票

以下对我有用(它打印两次)

© www.soinside.com 2019 - 2024. All rights reserved.