unique_ptr 클래스
소유한 개체에 대한 포인터를 저장합니다. 이 개체는 다른 unique_ptr에서 소유하지 않습니다. unique_ptr이 소멸되면 이 개체도 소멸됩니다.
template< class Type, class Del = default_delete<Type> >
class unique_ptr {
public:
unique_ptr( );
unique_ptr( nullptr_t Nptr );
explicit unique_ptr( pointer Ptr );
unique_ptr( pointer Ptr,
typename conditional<is_reference<Del>::value,
Del,
typename add_reference<const Del>::type>::type Deleter);
unique_ptr (pointer Ptr,
typename remove_reference<Del>::type&& Deleter);
unique_ptr (unique_ptr&& Right);
template<class Type2, Class Del2> unique_ptr( unique_ptr<Type2, Del2>&& Right );
unique_ptr( const unique_ptr& Right) = delete;
unique_ptr& operator=(const unique_ptr& Right ) = delete;
};
매개 변수
Right
unique_ptrNptr
std::nullptr_t 형식의 rvalue입니다.Ptr
pointerDeleter
unique_ptr에 바인딩되는 deleter 함수.
예외
unique_ptr에는 예외가 생성되지 않았습니다.
설명
unique_ptr 클래스는 auto_ptr을 대체하며 STL 컨테이너의 요소로 사용할 수 있습니다.
unique_ptr의 새 인스턴스를 효율적으로 만들려면 make_unique 도우미 함수를 사용하십시오.
unique_ptr은 리소스를 고유하게 관리합니다. 각 unique_ptr 개체는 null 포인터를 소유 또는 저장하는 개체에 대해 포인터를 저장합니다. 리소스는 둘 이상의 unique_ptr 개체가 소유할 수 없으며, 특정 리소스를 소유하는 unique_ptr 개체가 소멸되면 리소스가 해제됩니다. unique_ptr 개체는 이동할 수 있지만 복사할 수 없습니다. 자세한 내용은 Rvalue 참조 선언자: &&를 참조하십시오.
리소스는 특정 unique_ptr에 대해 리소스가 할당된 방식을 알고 있는 Del 형식의 저장된 deleter 개체를 호출하여 해제됩니다. 기본 deleter default_delete<Type>는 new를 사용하여 _Ptr에서 가리키는 리소스를 할당하고 delete _Ptr을 호출하여 해제할 수 있음을 가정합니다. (부분 특수화 **unique_ptr<Type[]>**는 new[]로 할당된 배열 개체를 관리하며, delete[] _Ptr을 호출하는 데 전문화된 기본 **deleterdefault_delete<Type[]>**이 있습니다.)
소유한 리소스에 대한 저장된 포인터, stored_ptr에는 pointer가 있습니다. 정의된 경우 Del::pointer이고, 정의되지 않은 경우 Type * 입니다. deleter가 상태 비저장일 경우 저장된 deleter 개체 stored_deleter는 개체에서 공간을 차지하지 않습니다. Del는 참조 형식이 될 수 있습니다.
멤버
생성자
unique_ptr의 8가지 생성자가 있습니다. |
형식 정의
템플릿 매개 변수 Del의 동의어. |
|
템플릿 매개 변수 Type의 동의어. |
|
Del::pointer의 동의어(정의된 경우)이며 그렇지 않은 경우 Type *. |
멤버 함수
stored_ptr를 반환합니다. |
|
stored_deleter에 대한 참조를 반환합니다. |
|
stored_ptr에 pointer()를 반환하고 이전 내용을 반환합니다. |
|
현재 소유한 리소스를 해제하고 새 리소스를 허용합니다. |
|
리소스를 교환하고 deleter를 제공된 unique_ptr로 교환합니다. |
연산자
operator bool |
연산자는 bool로 변환 가능한 형식의 값을 반환합니다. get() != pointer()일 경우 bool로 변환한 결과는 true입니다. 그렇지 않으면 false입니다. |
operator-> |
멤버 함수에서 stored_ptr을 반환합니다. |
operator* |
멤버 함수에서 stored_ptr을 반환합니다*. |
unique_ptr 값(또는 pointer-type) 값을 현재 unique_ptr에 할당합니다. |
요구 사항
헤더: <memory>
네임스페이스: std