Std::unique_ptr
std::unique_ptr is a C++ standard-library smart pointer that provides exclusive ownership of a dynamically allocated object and manages its lifetime.
100 C++ Mistakes and How to Avoid Them
std::unique_ptr is a C++ standard-library smart pointer that provides exclusive ownership of a dynamically allocated object or array and automatically manages its lifetime.
- How it is used
- Uses exclusive ownership to replace raw-pointer resource management and simplify exception-safe lifetime handling.
- How deep it goes
- Explains object and array ownership, destructor removal, make_unique construction, and std::move transfer, but not custom deleters.
- Examples
- Demonstrates Buffer objects, dynamically allocated arrays, a raw-pointer double delete, and transfer between two unique_ptr instances.
- What you can do after
- Apply it via std::make_unique and std::move to construct, own, and transfer dynamically allocated objects or arrays.
Read how 100 C++ Mistakes and How to Avoid Them explains std::unique_ptr →
Learn C++ by Example
std::unique_ptr is a C++ standard library smart pointer that exclusively owns a dynamically allocated object and destroys it when ownership ends.
- How it is used
- Manages exclusive dynamic ownership in containers, polymorphic objects, and coroutine tasks.
- How deep it goes
- Explains RAII, deletion, copying, moving, member access, containers, and custom deleters, but not implementation internals.
- Examples
- Uses smart-pointer vectors, a polymorphic blob race, make_unique construction, and coroutine-handle cleanup.
- What you can do after
- Apply it via the C++ standard library to express exclusive ownership and automatic cleanup, including custom destruction.
Related concepts
- std::threadequivalent to · A C++ standard library class that represents and manages a thread of execution.
- std::shared_ptralternative to · std::shared_ptr is a C++ standard-library smart pointer that enables multiple owners to share ownership of a dynamically allocated object.
- exclusive ownershipimplements · Exclusive ownership is a property in which one party has sole control or responsibility for a specified resource, right, work, or property.
- std::moveuses · std::move is a C++ standard-library function template that casts an expression to an rvalue reference, enabling move operations.
- resource acquisition is initializationimplements · Resource acquisition is initialization (RAII) is a resource-management pattern that ties resource ownership and release to the lifetime of an object or scope.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

