将std :: vector <std :: unique_ptr <int >>的所有权转移到正在构造的类的正确方法

Jen*_*Jen 23 c++ ownership stdvector unique-ptr c++11

将所有权转移std::vector<unique_ptr<int> >到正在建造的阶级的正确方法是什么?

下面是我想要做的代码表示.我意识到它不正确(不会编译)并违反"唯一性",无论我是通过值还是通过引用将向量传递给构造函数.我希望Foo成为向量的新所有者,并希望调用函数放弃所有权.我需要构造函数std::unique_ptr<std::vector<std::unique_ptr<int> > >来执行此操作吗?

foo.h中

class Foo
{
public:
  Foo(vector<std::unique_ptr<int> > vecOfIntPtrsOwnedByCaller);

private:
  vector<std::unique_ptr<int> > _vecOfIntPtrsOwnedByFoo;
}
Run Code Online (Sandbox Code Playgroud)

Foo.cpp中

Foo::Foo(std::vector<std::unique_ptr< int> > vecOfIntPtrsOwnedByCaller)
{
    _vecOfIntPtrsOwnedByFoo = vecOfIntPtrsOwnedByCaller;
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激 - 我已经在网上搜寻正确的方法来做到这一点.谢谢!

Die*_*ühl 22

std::unique_ptr<T>是一种不可复制但可移动的类型.只有移动类型std:vector<T>才能进行std::vector<T>移动.要让编译器自动移动对象,您需要为移动构造或移动分配设置r值.在你的构造函数中,对象vecOfIntPtrsOwnedByCaller是一个l值,尽管它尽管名称已经拥有指向的ints:当调用者创建对象时,它们从调用者那里"被盗".要从l值移动,您需要使用std::move()(或等效的):

Foo::Foo(std::vector<std::unique_ptr<int>> vecOfIntPtrsOwnedByCaller)
{
    _vecOfIntPtrsOwnedByFoo = std::move(vecOfIntPtrsOwnedByCaller);
}
Run Code Online (Sandbox Code Playgroud)

或者,最好

Foo::Foo(std::vector<std::unique_ptr<int>> vecOfIntPtrsOwnedByCaller)
    : _vecOfIntPtrsOwnedByFoo(std::move(vecOfIntPtrsOwnedByCaller))
{
}
Run Code Online (Sandbox Code Playgroud)

后一种方法避免首先默认构造成员,然后移动分配给它,而是移动 - 直接构造成员.我想,我也会把参数作为r值引用,但这不是必需的.

请注意,您Foo只能从可以绑定到r值的内容构造类型的对象,例如:

int main() {
    Foo f0(std::vector<std::unique_ptr<int>>()); // OK
    std::vector<std::unique_ptr<int>> v;
    Foo f1(v); v// ERROR: using with an l-value
    Foo f2{v}; v// ERROR: using with an l-value
    Foo f3 = v; // ERROR: using with an l-value
    Foo f4(std::move(v)); // OK: pretend that v is an r-value
}
Run Code Online (Sandbox Code Playgroud)