unique_ptr 클래스

소유한 개체 또는 배열에 대한 포인터를 저장합니다. 개체/배열은 다른 unique_ptr이 소유하지 않습니다. 개체/배열은 unique_ptr이 소멸될 때 소멸됩니다.

구문

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 T2, Class Del2>
    unique_ptr(unique_ptr<T2, Del2>&& Right);
    unique_ptr(const unique_ptr& Right) = delete;
    unique_ptr& operator=(const unique_ptr& Right) = delete;
};

//Specialization for arrays:
template <class T, class D>
class unique_ptr<T[], D> {
public:
    typedef pointer;
    typedef T element_type;
    typedef D deleter_type;
    constexpr unique_ptr() noexcept;
    template <class U>
    explicit unique_ptr(U p) noexcept;
    template <class U>
    unique_ptr(U p, see below d) noexcept;
    template <class U>
    unique_ptr(U p, see below d) noexcept;
    unique_ptr(unique_ptr&& u) noexcept;
    constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }     template <class U, class E>
        unique_ptr(unique_ptr<U, E>&& u) noexcept;
    ~unique_ptr();
    unique_ptr& operator=(unique_ptr&& u) noexcept;
    template <class U, class E>
    unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
    unique_ptr& operator=(nullptr_t) noexcept;
    T& operator[](size_t i) const;

    pointer get() const noexcept;
    deleter_type& get_deleter() noexcept;
    const deleter_type& get_deleter() const noexcept;
    explicit operator bool() const noexcept;
    pointer release() noexcept;
    void reset(pointer p = pointer()) noexcept;
    void reset(nullptr_t = nullptr) noexcept;
    template <class U>
    void reset(U p) noexcept = delete;
    void swap(unique_ptr& u) noexcept;  // disable copy from lvalue unique_ptr(const unique_ptr&) = delete;
    unique_ptr& operator=(const unique_ptr&) = delete;
};

매개 변수

Right
unique_ptr.

Nptr
rvalue 형식의 std::nullptr_t입니다.

Ptr
pointer.

Deleter
deleter에 바인딩되는 unique_ptr 함수.

예외

unique_ptr에는 예외가 생성되지 않았습니다.

설명

unique_ptr 클래스는 auto_ptr을 대체하며 C++ 표준 라이브러리 컨테이너의 요소로 사용할 수 있습니다.

도우미 함수를 make_unique 사용하여 새 인스턴스를 unique_ptr효율적으로 만듭니다.

unique_ptr은 리소스를 고유하게 관리합니다. 각 unique_ptr 개체는 null 포인터를 소유 또는 저장하는 개체에 대해 포인터를 저장합니다. 리소스는 둘 이상의 unique_ptr 개체가 소유할 수 없으며, 특정 리소스를 소유하는 unique_ptr 개체가 제거되면 리소스가 해제됩니다. 개체를 unique_ptr 이동할 수 있지만 복사할 수는 없습니다. 자세한 내용은 Rvalue 참조 선언자를 참조 하세요 &&.

리소스는 특정 deleter에 대해 리소스가 할당된 방식을 알고 있는 Del 형식의 저장된 unique_ptr 개체를 호출하여 해제됩니다. 기본 deleterdefault_delete<T>new를 사용하여 ptr에서 가리키는 리소스를 할당하며 delete _Ptr을 호출하여 해제할 수 있다고 가정합니다. (부분 특수화 unique_ptr<T[]>는 할당된 new[]배열 개체를 관리하며 기본값 deleterdefault_delete<T[]>은 호출 delete[] ptr하도록 특수화되어 있습니다.)

소유한 리소스에 대한 저장된 포인터, stored_ptr에는 pointer가 있습니다. Del::pointer 정의된 경우이며 T * 그렇지 않은 경우입니다. deleter가 상태 비저장일 경우 저장된 stored_deleter 개체 deleter는 개체에서 공간을 차지하지 않습니다. Del는 참조 형식이 될 수 있습니다.

멤버

생성자

이름 설명
unique_ptr unique_ptr의 8가지 생성자가 있습니다.

Typedef

이름 설명
deleter_type 템플릿 매개 변수 Del의 동의어.
element_type 템플릿 매개 변수 T의 동의어.
pointer Del::pointer의 동의어(정의된 경우)이며 그렇지 않은 경우 T *.

함수

속성 설명
get stored_ptr을(를) 반환합니다.
get_deleter stored_deleter에 대한 참조를 반환합니다.
release pointer()stored_ptr를 반환하고 이전 내용을 반환합니다.
reset 현재 소유한 리소스를 해제하고 새 리소스를 허용합니다.
swap 리소스를 교환하고 deleter를 제공된 unique_ptr로 교환합니다.

연산자

이름 설명
operator bool 연산자는 bool로 변환 가능한 형식의 값을 반환합니다. bool일 경우 true로 변환한 결과는 get() != pointer()입니다. 그렇지 않으면 false입니다.
operator-> 멤버 함수는 stored_ptr를 반환합니다.
operator* 멤버 함수는 *stored_ptr를 반환합니다.
operator= unique_ptr 또는 pointer-type의 값을 현재 unique_ptr에 할당합니다.

deleter_type

이 형식은 템플릿 매개 변수 Del의 동의어입니다.

typedef Del deleter_type;

설명

이 형식은 템플릿 매개 변수 Del의 동의어입니다.

element_type

이 형식은 템플릿 매개 변수 Type의 동의어입니다.

typedef Type element_type;

설명

이 형식은 템플릿 매개 변수 Ty의 동의어입니다.

get

stored_ptr을(를) 반환합니다.

pointer get() const;

설명

멤버 함수는 stored_ptr를 반환합니다.

get_deleter

stored_deleter에 대한 참조를 반환합니다.

Del& get_deleter();

const Del& get_deleter() const;

설명

멤버 함수는 stored_deleter에 대한 참조를 반환합니다.

operator=

제공된 unique_ptr의 주소를 현재 항목에 할당합니다.

unique_ptr& operator=(unique_ptr&& right);
template <class U, Class Del2>
unique_ptr& operator=(unique_ptr<Type, Del>&& right);
unique_ptr& operator=(pointer-type);

매개 변수

현재 unique_ptr의 값을 할당하는 데 사용된 unique_ptr 참조입니다.

설명

구성원 함수는 reset(right.release())를 호출하고 right.stored_deleterstored_deleter로 이동한 다음 *this를 반환합니다.

pointer

Del::pointer의 동의어(정의된 경우)이며 그렇지 않은 경우 Type *.

typedef T1 pointer;

설명

형식은 Del::pointer의 동의어(정의된 경우)이며 그렇지 않은 경우 Type *입니다.

release

호출자에 대해 저장된 포인터가 반환되면 소유권을 해제하고 저장된 포인터 값을 nullptr로 설정합니다.

pointer release();

설명

release를 사용하여 unique_ptr이 저장한 원시 포인터의 소유권을 가져올 수 있습니다. 호출자는 반환된 포인터를 삭제해야 합니다. unique-ptr은 비어 있는 기본 생성 상태로 설정됩니다. unique_ptr 호출 후에 호환되는 형식의 다른 포인터를 release에 할당할 수 있습니다.

예시

이 예에서는 릴리스의 호출자가 반환된 개체를 책임지는 방식을 보여 줍니다.

// stl_release_unique.cpp
// Compile by using: cl /W4 /EHsc stl_release_unique.cpp
#include <iostream>
#include <memory>

struct Sample {
   int content_;
   Sample(int content) : content_(content) {
      std::cout << "Constructing Sample(" << content_ << ")" << std::endl;
   }
   ~Sample() {
      std::cout << "Deleting Sample(" << content_ << ")" << std::endl;
   }
};

void ReleaseUniquePointer() {
   // Use make_unique function when possible.
   auto up1 = std::make_unique<Sample>(3);
   auto up2 = std::make_unique<Sample>(42);

   // Take over ownership from the unique_ptr up2 by using release
   auto ptr = up2.release();
   if (up2) {
      // This statement does not execute, because up2 is empty.
      std::cout << "up2 is not empty." << std::endl;
   }
   // We are now responsible for deletion of ptr.
   delete ptr;
   // up1 deletes its stored pointer when it goes out of scope.
}

int main() {
   ReleaseUniquePointer();
}
Constructing Sample(3)
Constructing Sample(42)
Deleting Sample(42)
Deleting Sample(3)

reset

포인터 매개 변수의 소유권을 가져온 후 원래 저장된 포인터를 삭제합니다. 새 포인터가 원래 저장된 포인터와 동일한 경우 reset은 포인터를 삭제하고 저장된 포인터를 nullptr로 설정합니다.

void reset(pointer ptr = pointer());
void reset(nullptr_t ptr);

매개 변수

ptr
소유권을 가져올 리소스에 대한 포인터입니다.

설명

to ptr에서 소유한 저장된 pointer 포인터를 unique_ptr 변경한 다음 원래 저장된 포인터를 삭제하는 데 사용합니다reset. unique_ptr 비어 reset 있지 않은 경우 원래 저장된 포인터에서 반환된 get_deleter deleter 함수를 호출합니다.

reset 먼저 새 포인터ptr를 저장한 다음 원래 저장된 포인터를 삭제하므로 원래 저장된 포인터와 동일한 경우 즉시 삭제 ptr 할 수 reset 있습니다.

swap

unique_ptr 개체 간에 포인터를 교환합니다.

void swap(unique_ptr& right);

매개 변수

right
포인터를 교환하는 데 사용되는 unique_ptr입니다.

설명

멤버 함수는 stored_ptrright.stored_ptr로 교환하고 stored_deleterright.stored_deleter로 교환합니다.

unique_ptr

unique_ptr의 8가지 생성자가 있습니다.

unique_ptr();

unique_ptr(nullptr_t);
explicit unique_ptr(pointer ptr);

unique_ptr(
    Type* 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 Ty2, Class Del2>
    unique_ptr(unique_ptr<Ty2, Del2>&& right);

매개 변수

ptr
에 할당할 리소스에 대한 포인터입니다 unique_ptr.

_Deleter
unique_ptr에 할당할 deleter입니다.

right
unique_ptr 필드가 새로 생성된 unique_ptr에 이동 할당되는 unique_ptr에 대한 rvalue reference입니다.

설명

처음 두 생성자가 리소스를 관리하지 않는 개체를 생성합니다. 세 번째 생성자는 ptr을 저장합니다 stored_ptr. 네 번째 생성자는 ptrstored_ptr에 저장하고 deleterstored_deleter에 저장합니다.

다섯 번째 생성자는 ptrstored_ptr에 저장하고 deleterstored_deleter로 이동합니다. 여섯 번째와 일곱 번째 생성자는 right.release()stored_ptr에 저장하고 right.get_deleter()stored_deleter로 이동합니다.

~unique_ptr

unique_ptr에 대한 소멸자는 unique_ptr 개체를 삭제합니다.

~unique_ptr();

설명

이 소멸자는 get_deleter()(stored_ptr)을 호출합니다.