简体   繁体   English

错误:调用 &#39;std::__1::unique_ptr 的隐式删除复制构造函数<A, std::__1::default_delete<A> &gt;&#39;

[英]error: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<A, std::__1::default_delete<A> >'

I'm constructing an object that takes a std::vector<std::unique_ptr<A> > as an argument.我正在构建一个以std::vector<std::unique_ptr<A> >作为参数的对象。 The constructor is defined like this构造函数是这样定义的

class B {
    std::vector <std::unique_ptr<A> > e_;

public:
    B(std::vector <std::unique_ptr<A> > e) : e_(std::move(e)){}

};

and then used as然后用作

std::vector <std::unique_ptr<A> > e;
B b(e);

and Xcode presents the error和 Xcode 出现错误

error: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<A, std::__1::default_delete<A> >'
:new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
                 ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

Why is the error still persisting even though i am using std::move() ?为什么即使我使用std::move()错误仍然存​​在?

EDIT: the error seems to vanish if i use B b(std::move(e)) instead of B b(e)) , is there any way to move the move logic to the implementation of the function?编辑:如果我使用B b(std::move(e))而不是B b(e)) ,错误似乎消失了,有没有办法将move逻辑move到函数的实现中?

Your constructor argument is pass by value which will make a copy, but you cannot copy a std::unique_ptr.您的构造函数参数是按值传递的,这将进行复制,但您不能复制 std::unique_ptr。 Passing by reference should work:通过引用传递应该有效:

class B {
    std::vector <std::unique_ptr<float> > e_;

public:
    B(std::vector <std::unique_ptr<float> >& e) : e_(std::move(e)){}

};

But...I agree with the other comments that this is bad design.但是......我同意其他评论,这是糟糕的设计。 If you want B to own e but also want to manipulate e outside of B then it should be a public member, no fancy constructor needed:如果你想让B拥有e但也想在B之外操作e那么它应该是一个公共成员,不需要花哨的构造函数:

class B {
public:
    std::vector <std::unique_ptr<float> > e_;
};

Why is the error still persisting even though i am using std::move()?为什么即使我使用 std::move() 错误仍然存​​在?

Because you are moving argument of ctor of B into member, which does not mean that variable e should or could be moved.因为您正在将B的 ctor 的参数移动到成员中,这并不意味着变量e应该或可以移动。

is there any way to move the move logic to the implementation of the function?有没有办法将移动逻辑移动到函数的实现中?

Even if it is possible, you should not do it.即使有可能,你也不应该这样做。 It should be clear for reader of code where e is used, that it was moved and cannot be used anymore.对于使用e的代码的读者来说,应该清楚它被移动并且不能再使用了。

The problem, as the previous answers suggest, is that you cannot copy a std::unique_ptr .正如前面的答案所暗示的那样,问题在于您不能复制std::unique_ptr Passing the constructor argument by value will trigger a copy.按值传递构造函数参数将触发复制。 That being said, if the intention is for B to own the unique_ptr , then the correct approach is to use B b(std::move(e)) .话虽如此,如果意图是让B拥有unique_ptr ,那么正确的方法是使用B b(std::move(e))

Contrary to some of the comments, this is actually the design recommended by Herb Sutter: https://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/ .与一些评论相反,这实际上是 Herb Sutter 推荐的设计: https : //herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/ Passing a unique_ptr by value makes it clear that the constructor takes ownership of the object away from the caller.通过值传递 unique_ptr 清楚地表明构造函数从调用者那里获得对象的所有权。 Furthermore, because the compiler requires an explicit std::move , the semantics are clearly documented in code.此外,由于编译器需要显式std::move ,因此代码中清楚地记录了语义。 That is, it is clear to users that the unique_ptr is invalidated by the constructor call because of the explicit std::move .也就是说,用户很清楚unique_ptr由于显式std::move被构造函数调用无效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向量 <unique_ptr<A> &gt;在构造函数中-错误:调用隐式删除的拷贝构造函数 - vector<unique_ptr<A> > in constructor - error: call to implicitly-deleted copy constructor 带矢量的unique_ptr:错误:调用XXX的隐式删除副本构造函数 - unique_ptr with vector: error: call to implicitly-deleted copy constructor of XXX 错误:使用auto调用unique_ptr的隐式删除的复制构造函数 - error: call to implicitly-deleted copy constructor of unique_ptr with auto 使用 unique_ptr 时调用隐式删除的复制构造函数 c++ 17 - call to implicitly-deleted copy constructor while using unique_ptr c++ 17 LLVM find_if具有unique_ptr &lt;&gt;的隐式删除副本构造函数 - LLVM find_if implicitly-deleted copy constructor with unique_ptr<> 二进制&#39;=&#39;:未找到采用&#39;std :: unique_ptr类型的右侧操作数的运算符 <char [],std::default_delete<_Ty> &gt;&#39; - binary '=': no operator found which takes a right-hand operand of type 'std::unique_ptr<char [],std::default_delete<_Ty>>' std :: unique_ptr,默认复制构造函数和抽象类 - std::unique_ptr, Default Copy Constructor, and Abstract Class 移动构造函数(错误:调用隐式删除的拷贝构造函数) - Move constructor (error: call to implicitly-deleted copy constructor) std::unique_ptr 的复制构造函数差异 - Copy constructor difference for std::unique_ptr 这个错误是什么意思?” 在模板中:调用 &#39;std::unique_ptr 的已删除构造函数<InventoryElements> &quot; - What does it means this error?" In template: call to deleted constructor of 'std::unique_ptr<InventoryElements>"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM