Подтвердить что ты не робот

Почему я не могу push_back 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;
}

Ошибка:

$ 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
4b9b3361

Ответ 1

Вам нужно переместить unique_ptr:

vec.push_back(std::move(ptr2x));

unique_ptr гарантирует, что один контейнер unique_ptr имеет право собственности на удерживаемый указатель. Это означает, что вы не можете сделать копии unique_ptr (потому что тогда два unique_ptr будут иметь право собственности), поэтому вы можете перемещать их только.

Обратите внимание, что ваше текущее использование unique_ptr неверно. Вы не можете использовать его для управления указателем на локальную переменную. Время жизни локальной переменной управляется автоматически: локальные переменные уничтожаются, когда заканчивается блок (например, когда функция возвращается в этом случае). Вам необходимо динамически выделить объект:

std::unique_ptr<int> ptr(new int(1));

Ответ 2

std:: unique_ptr не имеет конструктора копирования. Вы создаете экземпляр, а затем запрашиваете std::vector для копирования этого экземпляра во время инициализации.

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>]'

Класс удовлетворяет требованиям MoveConstructible и MoveAssignable, но не требования CopyConstructible или CopyAssignable.

Следующее работает с новыми вызовами emplace.

std::vector< std::unique_ptr< int > > vec;
vec.emplace_back( new int( 1984 ) );

См. с помощью unique_ptr со стандартными библиотечными контейнерами для дальнейшего чтения.