【发布时间】:2016-04-02 00:39:06
【问题描述】:
以下代码无法在 gcc 5.3 上编译,并出现编译器错误,抱怨以某种方式调用了 unique_ptr 的复制构造函数。有人可以解释为什么会这样吗?
#include <iostream>
#include <memory>
#include <deque>
using Foo = std::deque<std::unique_ptr<int>>;
void foo() {
std::vector<Foo> a;
a.emplace_back(); // this fails to compile
}
编译器错误的关键行是:
gcc-4.9.2/include/c++/4.9.2/bits/stl_construct.h:75:7: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]’ { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
【问题讨论】:
-
哦,是的,这是一团糟。
-
您应该包括
<vector>和<deque>,而不是<queue>。在这里并不重要。 -
感谢您指出这一点。固定的。顺便说一句,如果我们将 std::deque 替换为 std::queue,则代码无法编译为相同的编译器错误。
-
@BarryTheHatchet。在问题中添加了错误消息。谢谢!