< cpp‎ | memory
Revision as of 03:10, 7 January 2013 by Cubbi (Talk | contribs)


Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <memory>

<td></td> <td></td> <tr class="t-dcl ">

<td >

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

> class unique_ptr;

<td > (1) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
template <

    class T,
    class Deleter

> class unique_ptr<T[],Deleter>;

<td > (2) </td> <td > (since C++11) </td> </tr> Template:ddcl list end

std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope.

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 a dynamically-allocated array of objects (e.g. allocated with new[])



unique_ptr is neither copyable nor copy-assignable, so 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.

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)

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

Member functions

Template:cpp/memory/unique ptr/dcl list constructorTemplate:cpp/memory/unique ptr/dcl list destructorTemplate:cpp/memory/unique ptr/dcl list operator=Template:cpp/memory/unique ptr/dcl list releaseTemplate:cpp/memory/unique ptr/dcl list resetTemplate:cpp/memory/unique ptr/dcl list swapTemplate:cpp/memory/unique ptr/dcl list getTemplate:cpp/memory/unique ptr/dcl list get deleterTemplate:cpp/memory/unique ptr/dcl list operator boolTemplate:cpp/memory/unique ptr/dcl list operator*Template:cpp/memory/unique ptr/dcl list operator at
Single-object version, unique_ptr<T>
Array version, unique_ptr<T[]>

Non-member functions

Template:cpp/memory/unique ptr/dcl list operator cmpTemplate:cpp/memory/unique ptr/dcl list swap2

Helper classes

Template:cpp/memory/unique ptr/dcl list hash


#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
        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


f(const Foo&)
destroying p2...