class template


template <class T, class D = default_delete<T>> class unique_ptr;
array specialization
template <class T, class D> class unique_ptr<T[],D>;
Unique pointer
Manages the storage of a pointer, providing a limited garbage-collection facility, with little to no overhead over built-in pointers (depending on the deleter used).

These objects have the ability of taking ownership of a pointer: once they take ownership they manage the pointed object by becoming responsible for its deletion at some point.

unique_ptr objects automatically delete the object they manage (using a deleter) as soon as they themselves are destroyed, or as soon as their value changes either by an assignment operation or by an explicit call to unique_ptr::reset.

unique_ptr objects own their pointer uniquely: no other facility shall take care of deleting the object, and thus no other managed pointer should point to its managed object, since as soon as they have to, unique_ptr objects delete their managed object without taking into account whether other pointers still point to the same object or not, and thus leaving any other pointers that point there as pointing to an invalid location.

A unique_ptr object has two components:
  • a stored pointer: the pointer to the object it manages. This is set on construction, can be altered by an assignment operation or by calling member reset, and can be individually accessed for reading using members get or release.
  • a stored deleter: a callable object that takes an argument of the same type as the stored pointer and is called to delete the managed object. It is set on construction, can be altered by an assignment operation, and can be individually accessed using member get_deleter.

unique_ptr objects replicate a limited pointer functionality by providing access to its managed object through operators * and -> (for individual objects), or operator [] (for array objects). For safety reasons, they do not support pointer arithmetics, and only support move assignment (disabling copy assignments).

Template parameters

The type of the managed object, aliased as member type element_type.
Type of the callable object used as deleter, aliased as member type deleter_type.
By default, this is default_delete, which is a stateless functor that simply uses the global delete for the operation, with class causing no additional overhead overall over a built-in pointer.

Member types

The following aliases are member types of unique_ptr.

member typedefinitionnotes
element_typefirst template parameter (T)The type of the managed object
deleter_typesecond template parameter (D)The type of the stored deleter.
Defaults to default_delete<T>
pointerremove_reference<D>::type::pointer, if this type exists
T*, otherwise
The pointer type

Member functions

Exclusive for non-specialized version (single objects):

Exclusive for the version specialized for arrays with runtime length:

Non-member function overloads