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

用户名

我有一个std::vector<std::unique_ptr<MyClass>>,我正在将其分配给相同类型的第二个向量。

我收到此编译器错误:

/opt/gcc-8.2.0/include/c++/8.2.0/bits/stl_algobase.h:324:18: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = MyClass; _Dp = std::default_delete<MyClass>]'
        *__result = *__first;

这是因为要进行分配,我需要为定义一个移动分配运算符MyClass该类仅包含几个unordered_maps,一个集合和几个基元。没有指针成员。

谢尔盖

您无法将std::unique_ptr元素向量复制-分配给另一个向量,因为您无法复制std::unique_ptr自身。它是独一无二的!

您定义的任何操作都不MyClass能改变这一事实。

您可以从这样的向量移开-但这意味着原始向量将不再包含这些std::unique_ptr元素。

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

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

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

如何使用std :: vector <unique_ptr <T >>作为默认参数

std :: vector <unique_ptr <class T >>的动态初始化

参数std :: unique_ptr <T> &&的std :: move或std :: forward

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

如何初始化std :: unique_ptr <std :: unique_ptr <T> []>?

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

使用 std::unique_ptr<T>::unique_ptr` 在 Visual Studio 2013 中构建错误

为什么std :: shared_ptr <T> = std :: unique_ptr <T []>可以编译,而std :: shared_ptr <T []> = std :: unique_ptr <T []>不可以编译?

std :: unique_ptr <T>而不在堆上分配内存

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

在gcc 6中实现std :: unique_ptr <T []> :: reset

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

std :: remove_if来自std :: vector的多态std :: unique_ptr

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

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

如何检查std :: unique_ptr是否为空(如果它位于std :: vector中)?

使用`std :: unique_ptr`时`std :: vector`中的数据不同

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

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

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

使用std :: vector <double>访问由std :: unique_ptr <double [2]>管理的数据

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

std :: unique_ptr :: release()与std :: move()

std :: unique_ptr与std :: map

将std :: unique_ptr <uint8_t []>内容转储到文件

在std :: unordered_set <std :: unique_ptr>(C ++ 20)中找到指针T *