Namespaces
Variants
Views
Actions

std::unique_ptr

From cppreference.com
 
 
 
 
 
Defined in header <memory>
template<

    class T,
    class Deleter = std::default_delete<T>

> class unique_ptr;
(1) (since C++11)
template <

    class T,
    class Deleter

> class unique_ptr<T[],Deleter>;
(2) (since C++11)

std::unique_ptr is a smart pointer that:

  • retains sole ownership of an object through a pointer, and
  • destroys the pointed-to object when the unique_ptr goes out of scope.

unique_ptr is not copyable or copy-assignable, two instances of unique_ptr cannot manage the same object. A non-const unique_ptr can transfer the ownership of the managed object to another unique_ptr. A const std::unique_ptr cannot be transferred, limiting the lifetime of the managed object to the scope in which the pointer was created. When the unique_ptr is destroyed, it disposes of the object through Deleter.

There are two versions of std::unique_ptr:

1) manages the lifetime of a single object, e.g. allocated with new

2) manages the lifetime of an array with a runtime length, e.g. allocated with new[]

Typical uses of std::unique_ptr include

  • providing exception safety to classes and functions that handle objects with dynamic lifetime, by guaranteeing deletion on both normal exit and exit through exception.
  • passing ownership of uniquely-owned objects with dynamic lifetime into functions
  • acquiring ownership of uniquely-owned objects with dynamic lifetime from functions
  • as the element type in move-aware containers, such as std::vector, which hold pointers to dynamically-allocated objects, e.g. if polymorphic behavior is desired

Contents

[edit] Member types

Member type Definition
pointer std::remove_reference<D>::type::pointer if that type exists, otherwise T*
element_type T, the type of the object managed by this unique_ptr
deleter_type Deleter, the function object or lvalue reference to function or to function object, to be called from the destructor

[edit] Member functions

constructs a new unique_ptr
(public member function) [edit]
destructs the managed object if such is present
(public member function) [edit]
assigns the unique_ptr
(public member function) [edit]
Modifiers
returns a pointer to the managed object and releases the ownership
(public member function) [edit]
replaces the managed object
(public member function) [edit]
swaps the managed objects
(public member function) [edit]
Observers
returns a pointer to the managed object
(public member function) [edit]
returns the deleter that is used for destruction of the managed object
(public member function) [edit]
checks if there is associated managed object
(public member function) [edit]
Single-object version, unique_ptr<T>
dereferences pointer to the managed object
(public member function) [edit]
Array version, unique_ptr<T[]>
provides indexed access to the managed array
(public member function) [edit]

[edit] Non-member functions

compares to another unique_ptr or with nullptr
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]

[edit] Helper classes

hash support for std::unique_ptr
(class template specialization) [edit]

[edit] Example

#include <iostream>
#include <memory>
 
struct Foo {
    Foo() { std::cout << "Foo::Foo\n"; }
    ~Foo() { std::cout << "Foo::~Foo\n"; }
    void bar() { std::cout << "Foo::bar\n"; }
};
 
void f(const Foo &foo)
{
    std::cout << "f(const Foo&)\n";
}
 
int main()
{
    std::unique_ptr<Foo> p1(new Foo);  // p1 owns Foo
    if (p1) p1->bar();
 
    {
        std::unique_ptr<Foo> p2(std::move(p1));  // now p2 owns Foo
        f(*p2);
 
        p1 = std::move(p2);  // ownership returns to p1
        std::cout << "destroying p2...\n";
    }
 
    if (p1) p1->bar();
 
    // Foo instance is destroyed when p1 goes out of scope
}

Output:

Foo::Foo
Foo::bar
f(const Foo&)
destroying p2...
Foo::bar
Foo::~Foo