std::unique_ptr (3) - Linux Manuals
std::unique_ptr: std::unique_ptr
NAME
std::unique_ptr - std::unique_ptr
Synopsis
Defined in header <memory>
template<
class T, (1) (since C++11)
class Deleter = std::default_delete<T>
> class unique_ptr;
template <
class T, (2) (since C++11)
class Deleter
> class unique_ptr<T[], Deleter>;
std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope.
The object is disposed of using the associated deleter when either of the following happens:
* the managing unique_ptr object is destroyed
* the managing unique_ptr object is assigned another pointer via operator= or reset().
The object is disposed of using a potentially user-supplied deleter by calling get_deleter()(ptr). The default deleter uses the delete operator, which destroys the object and deallocates the memory.
A unique_ptr may alternatively own no object, in which case it is called empty.
There are two versions of std::unique_ptr:
1) Manages a single object (e.g. allocated with new)
2) Manages a dynamically-allocated array of objects (e.g. allocated with new[])
The class satisfies the requirements of MoveConstructible and MoveAssignable, but not the requirements of either CopyConstructible or CopyAssignable.
Type requirements
-
Deleter must be FunctionObject or lvalue reference to a FunctionObject or lvalue reference to function, callable with an argument of type unique_ptr<T, Deleter>::pointer
Notes
Only non-const unique_ptr can transfer the ownership of the managed object to another unique_ptr. If an object's lifetime is managed by a const std::unique_ptr, it is limited to the scope in which the pointer was created.
std::unique_ptr is commonly used to manage the lifetime of objects, including:
* 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)
std::unique_ptr may be constructed for an incomplete_type T, such as to facilitate the use as a handle in the pImpl_idiom. If the default deleter is used, T must be complete at the point in code where the deleter is invoked, which happens in the destructor, move assignment operator, and reset member function of std::unique_ptr. (Conversely, std::shared_ptr can't be constructed from a raw pointer to incomplete type, but can be destroyed where T is incomplete). Note that if T is a class template specialization, use of unique_ptr as an operand, e.g. !p requires T's parameters to be complete due to ADL.
If T is a derived_class of some base B, then std::unique_ptr<T> is implicitly_convertible to std::unique_ptr<B>. The default deleter of the resulting std::unique_ptr<B> will use operator_delete for B, leading to undefined_behavior unless the destructor of B is virtual. Note that std::shared_ptr behaves differently: std::shared_ptr<B> will use the operator_delete for the type T and the owned object will be deleted correctly even if the destructor of B is not virtual.
Unlike std::shared_ptr, std::unique_ptr may manage an object through any custom handle type that satisfies NullablePointer. This allows, for example, managing objects located in shared memory, by supplying a Deleter that defines typedef boost::offset_ptr pointer; or another fancy_pointer.
Member types
Member type Definition
pointer std::remove_reference<Deleter>::type::pointer if that type exists, otherwise T*. Must satisfy NullablePointer
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
constructor (public member function)
destructor (public member function)
operator= (public member function)
Modifiers
release (public member function)
reset (public member function)
swap (public member function)
Observers
get (public member function)
get_deleter (public member function)
operator_bool (public member function)
Single-object version, unique_ptr<T>
operator* (public member function)
operator->
Array version, unique_ptr<T[]>
operator[] (public member function)
Non-member functions
make_unique
make_unique_default_init creates a unique pointer that manages a new object
(C++14)
(C++20)
operator==
operator!= compares to another unique_ptr or with nullptr
operator< (function template)
operator<=
operator>
operator>=
operator<< outputs the value of the managed pointer to an output stream
(C++20)
std::swap(std::unique_ptr) specializes the std::swap algorithm
(C++11)
Helper classes
std::hash<std::unique_ptr> hash support for std::unique_ptr