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++ 標準ライブラリ コンテナーの要素として使用できます。

効率的に unique_ptr の新しいインスタンスを作成するには、make_unique ヘルパー関数を使用します。

unique_ptr は一意にリソースを管理します。 unique_ptr オブジェクトは null ポインターを所有または保存するオブジェクトへのポインターを格納します。 1 つのリソースは、1 つの unique_ptr オブジェクトによってのみ所有されます。特定のリソースを所有する unique_ptr オブジェクトが破棄された時点で、リソースが解放されます。 unique_ptr オブジェクトを移動することはできますが、コピーすることはできません。詳細については、「右辺値参照宣言子: &&」を参照してください。

特定の deleter に対するリソースの割り当てを認識する、Del 型の格納された unique_ptr オブジェクトを呼び出すことによって、リソースが解放されます。 既定の deleterdefault_delete<T> は、ptr が指すリソースが new で割り当てられることと、delete _Ptr を呼び出すことによって解放できることを前提としています。 (部分的特殊化 unique_ptr<T[]>new[] で割り当てられた配列オブジェクトを管理します。また、delete[] ptr の呼び出しに特化した既定の deleterdefault_delete<T[]> があります。)

所有されたリソースに対する格納されたポインター、stored_ptr には、pointer 型があります。 定義されている Del::pointer 場合と T * 定義されていない場合です。 deleter がステートレスである場合、格納された stored_deleter オブジェクトである deleter はオブジェクト内の領域を使用しません。 Del が参照型である場合があることに注意してください。

メンバー

コンストラクター

名前 説明
unique_ptr unique_ptr には、7 種類のコンストラクターがあります。

Typedefs

名前 説明
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_ptrunique_ptr (または pointer-type) の値を割り当てます。

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 に割り当てることができます。

この例は、返されたオブジェクトに 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
所有権を取得するリソースへのポインター。

解説

reset を使用して、unique_ptr が所有する格納されている pointerptr に変更してから、元の格納ポインターを削除します。 空 unique_ptr でない場合は、 reset 元の格納されているポインターで get_deleter 返される deleter 関数を呼び出します。

最初に新しいポインターptrを格納し、次に元の格納されたポインターを削除するためreset、元の格納されているポインターと同じ場合は、すぐに削除ptrできますreset

swap

2 つの unique_ptr オブジェクト間でポインターを交換します。

void swap(unique_ptr& right);

パラメーター

right
ポインターを交換するために使用される unique_ptr

解説

このメンバー関数は、stored_ptrright.stored_ptr と交換し、stored_deleterright.stored_deleter と交換します。

unique_ptr

unique_ptr には、7 種類のコンストラクターがあります。

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

解説

最初の 2 つのコンストラクターはリソースを管理しないオブジェクトを構築します。 3 番目のコンストラクターは stored_ptrptr を格納します。 4 番目のコンストラクターは stored_ptrptr を、stored_deleterdeleter を格納します。

5 番目のコンストラクターは stored_ptrptr を格納し、stored_deleterdeleter を移動します。 6 番目と 7 番目のコンストラクターは stored_ptrright.release() を格納し、stored_deleterright.get_deleter() を移動します。

~unique_ptr

unique_ptr のデストラクターで、unique_ptr オブジェクトを破棄します。

~unique_ptr();

解説

このデストラクターは get_deleter()(stored_ptr) を呼び出します。