반응형
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:\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: 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:\mingw\bin\../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:\mingw\bin\../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:\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: 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:\mingw\bin\../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:\mingw\bin\../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:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/vector.tcc:314:4: error: used here
당신은 이동해야합니다 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));
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 사용을 참조하십시오 .
참고 URL : https://stackoverflow.com/questions/3283778/why-can-i-not-push-back-a-unique-ptr-into-a-vector
반응형
'Programming' 카테고리의 다른 글
Android-패키지 이름 규칙 (0) | 2020.05.13 |
---|---|
PHP에서 echo, print 및 print_r의 차이점은 무엇입니까? (0) | 2020.05.13 |
JavaScript에 Sleep / Pause / Wait 기능이 있습니까? (0) | 2020.05.13 |
쿼리에서 반환 된 각 행에 대해 저장 프로 시저를 어떻게 한 번 실행합니까? (0) | 2020.05.13 |
일부 스크립트에서 닫는 PHP 태그 '?>'를 생략하는 이유는 무엇입니까? (0) | 2020.05.13 |