std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer. unique_ptr is not copyable or copy-assignable, two instances of unique_ptr cannot manage the same object.
std::unique_ptr
is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when theunique_ptr
goes out of scope. No twounique_ptr
instances can manage the same object.
std::unique_ptr
was designed to replace the std::auto_ptr
in C++03. It improves on the implementation of auto_ptr
by implementing specific move semantics (it is not copyable) that were not available in the language of C++03.
std::unique_ptr
, together with std::shared_ptr
and (std::weak_ptr
) form the core smart pointers used in C++ to implement RAII semantics, especially with respect to traditional memory management. With custom deleters, these smart pointers can also be used to manage other resources.
Resources: