Std::unique_ptr
std::unique_ptr is a C++ standard-library class template that exclusively owns a dynamically allocated object or array 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 std::unique_ptr to replace manual management of exclusively owned dynamic resources.
- How deep it goes
- Works through object and array ownership, automatic cleanup, and ownership transfer with std::move.
- Examples
- Contrasts a double-deleted raw Buffer with a Buffer using unique pointers for itself and its array.
- What you can do after
- Apply std::unique_ptr and std::make_unique to manage an exclusive resource and transfer its ownership.
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.
What to know first
- destructorA destructor is an operation associated with an object or value that performs cleanup when its lifetime ends.
- moveMove semantics are language rules that transfer a value or its resources to another binding or object without copying them, potentially making the original binding unavailable.
- referenceA reference is an indirect value, binding, or relationship that identifies or provides access to another object, value, resource, or source.
Related concepts
- exclusive ownershipimplements · Exclusive ownership is a property in which one party has sole control or responsibility for a specified resource, right, work, or property.
- make_uniqueuses · std::make_unique is a C++ standard-library function template that constructs an object and returns a std::unique_ptr that owns it.
- referenceuses · A reference is an indirect value, binding, or relationship that identifies or provides access to another object, value, resource, or source.
- 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.
- smart pointerkind of · A smart pointer is a pointer-like object that manages or mediates access to a referenced resource, often providing ownership, lifetime, or safety behavior.
Questions about std::unique_ptr
What is std::unique_ptr?
std::unique_ptr is a C++ standard-library class template that exclusively owns a dynamically allocated object or array and manages its lifetime.
Which Manning books explain std::unique_ptr?
100 C++ Mistakes and How to Avoid Them and Learn C++ by Example.
What should I know before learning std::unique_ptr?
Manning books that teach std::unique_ptr usually introduce destructor, move and reference first.
What is std::unique_ptr a kind of?
Std::unique_ptr is a kind of smart pointer. A smart pointer is a pointer-like object that manages or mediates access to a referenced resource, often providing ownership, lifetime, or safety behavior.
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.

