为什么我不能 unique_ptr 推回到向量?
问题说明
这个程序有什么问题?
#include <memory>
#include <vector>
int main()
{
std::vector<std::unique_ptr<int>> vec;
int x(1);
std::unique_ptr<int> ptr2x(&x);
vec.push_back(ptr2x); //This tiny command has a vicious error.
return 0;
}
错误:
In file included from c:mingwin../lib/gcc/mingw32/4.5.0/include/c /mingw32/bits/c allocator.h:34:0,
from c:mingwin../lib/gcc/mingw32/4.5.0/include/c /bits/allocator.h:48,
from c:mingwin../lib/gcc/mingw32/4.5.0/include/c /memory:64,
from main.cpp:6:
c:mingwin../lib/gcc/mingw32/4.5.0/include/c /bits/unique_ptr.h: In member function 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::unique_ptr<int>, _Tp* = std::unique_ptr<int>*]':
c:mingwin../lib/gcc/mingw32/4.5.0/include/c /bits/stl_vector.h:745:6: instantiated from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::unique_ptr<int>, _Alloc = std::allocator<std::unique_ptr<int> >, value_type = std::unique_ptr<int>]'
main.cpp:16:21: instantiated from here
c:mingwin../lib/gcc/mingw32/4.5.0/include/c /bits/unique_ptr.h:207:7: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = int, _Tp_Deleter = std::default_delete<int>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<int>]'
c:mingwin../lib/gcc/mingw32/4.5.0/include/c /ext/new_allocator.h:105:9: error: used here
In file included from c:mingwin../lib/gcc/mingw32/4.5.0/include/c /vector:69:0,
from main.cpp:7:
c:mingwin../lib/gcc/mingw32/4.5.0/include/c /bits/unique_ptr.h: In member function 'void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {const std::unique_ptr<int>&}, _Tp = std::unique_ptr<int>, _Alloc = std::allocator<std::unique_ptr<int> >, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<std::unique_ptr<int>*, std::vector<std::unique_ptr<int> > >, typename std::vector<_Tp, _Alloc>::_Base::_Tp_alloc_type::pointer = std::unique_ptr<int>*]':
c:mingwin../lib/gcc/mingw32/4.5.0/include/c /bits/stl_vector.h:749:4: instantiated from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::unique_ptr<int>, _Alloc = std::allocator<std::unique_ptr<int> >, value_type = std::unique_ptr<int>]'
main.cpp:16:21: instantiated from here
c:mingwin../lib/gcc/mingw32/4.5.0/include/c /bits/unique_ptr.h:207:7: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = int, _Tp_Deleter = std::default_delete<int>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<int>]'
c:mingwin../lib/gcc/mingw32/4.5.0/include/c /bits/vector.tcc:314:4: error: used here
正确答案
你需要移动 unique_ptr
:
You need to move the unique_ptr
:
vec.push_back(std::move(ptr2x));
unique_ptr
保证单个 unique_ptr
容器拥有持有的指针的所有权.这意味着您不能复制 unique_ptr
(因为两个 unique_ptr
将拥有所有权),因此您只能移动它.
unique_ptr
guarantees that a single unique_ptr
container has ownership of the held pointer. This means that you can't make copies of a unique_ptr
(because then two unique_ptr
s would have ownership), so you can only move it.
但是请注意,您当前使用的 unique_ptr
是不正确的.您不能使用它来管理指向局部变量的指针.局部变量的生命周期是自动管理的:局部变量在块结束时被销毁(例如,在这种情况下,当函数返回时).您需要动态分配对象:
Note, however, that your current use of unique_ptr
is incorrect. You cannot use it to manage a pointer to a local variable. The lifetime of a local variable is managed automatically: local variables are destroyed when the block ends (e.g., when the function returns, in this case). You need to dynamically allocate the object:
std::unique_ptr<int> ptr(new int(1));
在 C 14 中,我们有一个更好的方法:
In C 14 we have an even better way to do so:
make_unique<int>(5);
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangicakb
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20