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

只是

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

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

oo

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

private:
  vector<std::unique_ptr<int> > _vecOfIntPtrsOwnedByFoo;
}

Foo.cpp

Foo::Foo(std::vector<std::unique_ptr< int> > vecOfIntPtrsOwnedByCaller)
{
    _vecOfIntPtrsOwnedByFoo = vecOfIntPtrsOwnedByCaller;
}

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

迪特玛·库尔(DietmarKühl)

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);
}

或者,更可取

Foo::Foo(std::vector<std::unique_ptr<int>> vecOfIntPtrsOwnedByCaller)
    : _vecOfIntPtrsOwnedByFoo(std::move(vecOfIntPtrsOwnedByCaller))
{
}

后一种方法避免首先默认构造成员,然后再对其进行移动分配,而是直接移动构造成员。我想,我也将参数设为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
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将所有权从std :: vector转移到std :: shared_ptr

std :: vector <std :: unique_ptr <int>>不编译

将std :: unique_ptr传递给构造函数以获取所有权

如何正确地将所有权从原始指针移到std :: unique_ptr?

std :: unique_ptr转移const对象的所有权

如何调整std :: vector <std :: queue <std :: unique_ptr <int >>>的大小?

'operator[]' 不匹配(操作数类型是 'std::unique_ptr<std::vector<int> >' 和 'int')

是否可以将所有权从void *转移到unique_ptr?

将原始指针的所有权转移到unique_ptr

如何取得std :: unique_ptr和std :: shared_ptr的所有权

如何在使用基于范围的 for 循环遍历 std::unique_ptr 的 std::vector 时获取对象的所有权?

构造类成员std :: vector <std :: unique_ptr <AClass>的智能方法

是否可以使用fill构造函数创建std :: vector <std :: unique_ptr <Bar >>?

std :: vector <std :: unique_ptr <>> :: push_back()的正确语法是什么?

声明包含std :: unique_ptr的结构的std :: vector类时出错

还有什么更好的替代std :: vector <std :: unique_ptr <T >>吗?

调整大小std :: vector <std :: unique_ptr <T >>的性能

取得所有权之前先进行std :: unique_ptr测试

将std :: vector <std :: unique_ptr <T >>分配给另一个std :: vector <std :: unique_ptr <T >>

如何构造一个具有“unique_ptr”作为成员变量的对象的“std::vector”?

std :: unique_ptr constexpr构造函数

std :: unique_ptr构造函数的行为

将std :: unique_ptr推回std :: vector时,编译器不会失败

将std :: unique_ptr类成员标记为const

将std :: unique_ptr传递给类时出错

C ++-vector <>中的std :: unique_ptr为nullptr

为什么我不能使用{std :: move(first),std :: move(second)}实例化std :: vector <std :: unique_ptr <int >>?

有没有安全的方法可以遍历std :: unique_ptr <int []>?

使用std :: unique_ptr <T>&代替std :: unique_ptr <T>有什么优势吗?