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. Unlike std::shared_ptr, std::unique_ptr may manage an object through any custom handle type that satisfies NullablePointer. Note that std::shared_ptr behaves differently: std:: shared_ptr 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. The default deleter of the resulting std :: unique_ptr will use operator delete for B, leading to undefined behavior unless the destructor of B is virtual. If T is a derived class of some base B, then std :: unique_ptr is implicitly convertible to std :: unique_ptr. !p requires T's parameters to be complete due to ADL. Note that if T is a class template specialization, use of unique_ptr as an operand, e.g. (Conversely, std::shared_ptr can't be constructed from a raw pointer to incomplete type, but can be destroyed where T is incomplete).
Unique code#
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. Std::unique_ptr may be constructed for an incomplete type T, such as to facilitate the use as a handle in the pImpl idiom. as the element type in move-aware containers, such as std::vector, which hold pointers to dynamically-allocated objects (e.g.acquiring ownership of uniquely-owned objects with dynamic lifetime from functions.passing ownership of uniquely-owned objects with dynamic lifetime into functions.providing exception safety to classes and functions that handle objects with dynamic lifetime, by guaranteeing deletion on both normal exit and exit through exception.Std::unique_ptr is commonly used to manage the lifetime of objects, including: 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. Only non-const unique_ptr can transfer the ownership of the managed object to another unique_ptr. The class satisfies the requirements of MoveConstructible and MoveAssignable, but of neither Cop圜onstructible nor CopyAssignable. Manages a dynamically-allocated array of objects (e.g.There are two versions of std::unique_ptr:
The default deleter uses the delete operator, which destroys the object and deallocates the memory.Ī unique_ptr may alternatively own no object, in which case it is called empty. The object is disposed of, using a potentially user-supplied deleter by calling get_deleter ( ) (ptr ). the managing unique_ptr object is assigned another pointer via operator= or reset().the managing unique_ptr object is destroyed.The object is disposed of, using the associated deleter when either of the following happens:
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.