为什么我不能push_back一个unique_ptr到向量? [英] Why can I not push_back a unique_ptr into a vector?
问题描述
这个程式有什么问题?
#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;
}
错误:
$ g++ -std=gnu++0x main.cpp
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c+
+/mingw32/bits/c++allocator.h:34:0,
from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c+
+/bits/allocator.h:48,
from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c+
+/memory:64,
from main.cpp:6:
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h: I
n member function 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*,
const _Tp&) [with _Tp = std::unique_ptr<int>, _Tp* = std::unique_ptr<
int>*]':
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_vector.h:74
5:6: instantiated from 'void std::vector<_Tp, _Alloc>::push_back(con
st value_type&) [with _Tp = std::unique_ptr<int>, _Alloc = std::alloca
tor<std::unique_ptr<int> >, value_type = std::unique_ptr<int>]'
main.cpp:16:21: instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h:20
7:7: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::uniqu
e_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = int, _Tp_D
eleter = std::default_delete<int>, std::unique_ptr<_Tp, _Tp_Deleter> =
std::unique_ptr<int>]'
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/ext/new_allocator.h:
105:9: error: used here
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c+
+/vector:69:0,
from main.cpp:7:
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h: I
n member function 'void std::vector<_Tp, _Alloc>::_M_insert_aux(std::v
ector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {const std::u
nique_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::p
ointer = std::unique_ptr<int>*]':
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_vector.h:74
9:4: instantiated from 'void std::vector<_Tp, _Alloc>::push_back(con
st value_type&) [with _Tp = std::unique_ptr<int>, _Alloc = std::alloca
tor<std::unique_ptr<int> >, value_type = std::unique_ptr<int>]'
main.cpp:16:21: instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h:20
7:7: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::uniqu
e_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = int, _Tp_D
eleter = std::default_delete<int>, std::unique_ptr<_Tp, _Tp_Deleter> =
std::unique_ptr<int>]'
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/vector.tcc:314:
4: error: used here
推荐答案
您需要移动 unique_ptr
: / p>
You need to move the unique_ptr
:
vec.push_back(std::move(ptr2x));
unique_ptr
$ c> 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));
这篇关于为什么我不能push_back一个unique_ptr到向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!