- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
以下片段:
#include <memory>
#include <utility>
namespace foo
{
template <typename T>
void swap(T& a, T& b)
{
T tmp = std::move(a);
a = std::move(b);
b = std::move(tmp);
}
struct bar { };
}
void baz()
{
std::unique_ptr<foo::bar> ptr;
ptr.reset();
}
不为我编译:
$ g++ -std=c++11 -c foo.cpp
In file included from /usr/include/c++/5.3.0/memory:81:0,
from foo.cpp:1:
/usr/include/c++/5.3.0/bits/unique_ptr.h: In instantiation of ‘void std::unique_ptr<_Tp, _Dp>::reset(std::unique_ptr<_Tp, _Dp>::pointer) [with _Tp = foo::bar; _Dp = std::default_delete<foo::bar>; std::unique_ptr<_Tp, _Dp>::pointer = foo::bar*]’:
foo.cpp:20:15: required from here
/usr/include/c++/5.3.0/bits/unique_ptr.h:342:6: error: call of overloaded ‘swap(foo::bar*&, foo::bar*&)’ is ambiguous
swap(std::get<0>(_M_t), __p);
^
In file included from /usr/include/c++/5.3.0/bits/stl_pair.h:59:0,
from /usr/include/c++/5.3.0/bits/stl_algobase.h:64,
from /usr/include/c++/5.3.0/memory:62,
from foo.cpp:1:
/usr/include/c++/5.3.0/bits/move.h:176:5: note: candidate: void std::swap(_Tp&, _Tp&) [with _Tp = foo::bar*]
swap(_Tp& __a, _Tp& __b)
^
foo.cpp:7:10: note: candidate: void foo::swap(T&, T&) [with T = foo::bar*]
void swap(T& a, T& b)
我声明一个 swap()
函数如此笼统以至于与 std::swap
冲突,这是我的错吗?
如果是这样,有没有办法定义 foo::swap()
以便它不会被 Koenig 查找拖入?
最佳答案
unique_ptr<T>
需要 T*
成为 NullablePointer
[unique.ptr]p3NullablePointer
需要 T*
的左值成为 Swappable
[nullablepointer.requirements]p1Swappable
本质上需要 using std::swap; swap(x, y);
为 x
选择过载, y
是 T*
类型的左值[swappable.requirements]p3在最后一步,您的类型 foo::bar
产生歧义,因此违反 unique_ptr
的要求. libstdc++ 的实现是符合标准的,尽管我会说这相当令人惊讶。
措辞当然有点复杂,因为它是通用的。
[unique.ptr]p3
If the type
remove_reference_t<D>::pointer
exists, thenunique_ptr<T, D>::pointer
shall be a synonym forremove_reference_t<D>::pointer
. Otherwiseunique_ptr<T,
shall be a synonym for
D>::pointerT*
. The typeunique_ptr<T,
shall satisfy the requirements of
D>::pointerNullablePointer
.
(强调我的)
[nullablepointer.requirements]p1
A
NullablePointer
type is a pointer-like type that supports null values. A typeP
meets the requirements ofNullablePointer
if:
- [...]
- lvalues of type
P
are swappable (17.6.3.2),- [...]
[swappable.requirements]p2
An object
t
is swappable with an objectu
if and only if:
- the expressions
swap(t, u)
andswap(u, t)
are valid when evaluated in the context described below, and- [...]
[swappable.requirements]p3
The context in which
swap(t, u)
andswap(u, t)
are evaluated shall ensure that a binary non-member function named “swap” is selected via overload resolution on a candidate set that includes:
- the two
swap
function templates defined in<utility>
and- the lookup set produced by argument-dependent lookup.
请注意,对于指针类型 T*
,对于 ADL,相关的命名空间和类派生自类型 T
.因此,foo::bar*
有 foo
作为关联的命名空间。 swap(x, y)
的 ADL其中x
或 y
是 foo::bar*
因此会找到 foo::swap
.
关于c++ - 可以定义一个完全通用的 swap() 函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36850522/
我的问题很简单。执行以下操作是否安全? 不需要任何道德建议,例如“不要将函数命名为 swap()!”或者随便什么,拜托! file1.hpp //header guards here #include
我很好奇,所以我检查了swap的源代码! clojure 存储库上的函数,如下所示: (defn swap! "Atomically swaps the value of atom to be:
我正在实现一个与 std::array 具有类似接口(interface)的类,它同时具有 member swap()和 non-member swap() . 由于我希望我的类模仿标准容器,因此我想
这个问题在这里已经有了答案: Rationale behind member function swap (1 个回答) 关闭 4 年前。 如果Coll是标准容器类型,a和b是Coll的实例;那么,
int a=5; b=7; int *pa=&a, *pb=&b; 交换 a 和 b 的值与交换指针的值有何不同,即 pa和 pb (不是 *pa 和 *pb )?两种情况下的结果不一样吗? 我们什么
好的,这是程序,绝对正确 #include using namespace std; template void Swap(T &a , T &b); int main(){ int i
std::swap() 函数是否可以在具有各种不同对象作为变量成员的类中正常工作?特别是,如果其中一些成员是智能指针? class test { ... std::shared_ptr
我正在尝试实现 copy-and-swap idiom在我的自定义 Matrix 类中,我按照链接问题中建议的方式执行 swap() 时遇到了一些麻烦: (我使用的编译器是MS VS2010 IDE的
我知道 ADL 是什么,我知道在 C++ 中,内部作用域函数隐藏外部作用域函数。也就是说,名称不会跨范围重载。所以函数重载需要在相同的范围内完成。 现在我的问题是,对于这个通用代码片段: #inclu
给定两个 std::vector v1, v2。 我想知道使用 std::swap(v1, v2) 比 v1.swap(v2) 有什么好处。 我已经实现了一个关于性能观点的简单测试代码(我不确定它是否
std::string 有一个成员函数 swap,这是交换两个大字符串的有效方法。 既然我们有了 move 语义,那么,给定两个大字符串 s1 和 s2,以下代码 s1.swap(s2); 在效率方面
同时使用 copy-and-swap idiom在一个具有常量引用作为成员的类中, 出现上述错误。 示例代码: #include #include using std::reference_wra
背景 考虑以下代码: #include namespace ns { struct foo { foo() : i(0) {} int i;
我们有: vector f(int); vector v; 这个有效: f(x).swap(v); 这不是: v.swap(f(x)); 为什么? 最佳答案 swap()对 vector 进行非常量引
我已在 Azure 中配置了一个生产和暂存槽,每个槽都使用自己的 SQL Azure 数据库。 每个连接字符串都配置为“粘性”。 但是,当我将生产槽(目标)与暂存槽(源)交换时,我收到以下警告。 这条
我有以下代码: char swap(char reg, char* mem) { std::swap(reg, *mem); return reg; } 我希望这可以编译为: swap
我使用 xor-swap 的插入排序不工作,但没有 xor-swap 它工作正常。如何修复我的异或交换插入排序算法? 没有异或交换的插入排序 - //sorts the given array in
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我正在实现 copy-and-swap idiom对于我设计的一个小型非拥有内存引用对象的 operator=。当 MemRef 引用我信任其生命周期的一 block 缓冲区时,_ptr 指向缓冲区,
我有两个问题,第二个是可选的。首先,在下面的程序(一个简单的卡片程序的原型(prototype))中,我得到以下错误: (29): error C2660: 'shuffle' : function
我是一名优秀的程序员,十分优秀!