Unique pointer
std::unique_ptr<T> is a move-only smart pointer that owns a heap object. When the unique_ptr dies, it deletes the object. There is no shared ownership and no reference count.
Detailed Explanation
A raw T* does not say who frees the object. unique_ptr<T> does: this handle is the owner. Copying is deleted. Moving transfers ownership and leaves the source empty (nullptr). That is the type you want for a factory return, a polymorphic Shape in a vector, and a PIMPL pointer.
Create with std::make_unique<T>(args) (C++14). That is one allocation and exception-safe. unique_ptr<T>(new T(args)) is legal and inferior (make_unique is the page next door). Arrays: make_unique<T[]>(n) and unique_ptr<T[]> which calls delete[].
get() returns the raw pointer without releasing ownership. operator* and operator-> dereference. release() gives you the raw pointer and empties the unique_ptr — you now own a raw pointer again, which is usually a step backward. reset(p) deletes the current object and takes p. reset() deletes and becomes empty.
A custom deleter is the second template argument: unique_ptr<FILE, FileCloser>. The deleter is stored (empty-base optimised if it is stateless) and called instead of delete. That is how you wrap fopen/fclose or a C API handle without writing a class.
unique_ptr<Base> can hold a Derived if Base has a virtual destructor. Without it, delete through the unique_ptr is the same UB as delete through a Base*.
Pass unique_ptr<T> by value when you transfer ownership. Pass const unique_ptr<T>& or (better) T* / T& when you only observe. A function that takes unique_ptr<T>& and does not reset it is lying about ownership; take T&.
vector<unique_ptr<T>> is the default heterogeneous list. It is movable. It is not copyable. That is what you want.
Cost: one pointer-sized object, no extra heap word for a refcount. Dereference is a load of the pointer. The destructor is a null check and a delete.
Code Examples
#include <iostream>
#include <memory>
struct Shape {
virtual ~Shape() = default;
virtual const char* name() const = 0;
};
struct Circle : Shape {
const char* name() const override { return "circle"; }
};
int main() {
std::unique_ptr<Shape> s = std::make_unique<Circle>();
std::cout << s->name() << '\n'; // prints: circle
auto t = std::move(s); // s is empty
std::cout << std::boolalpha << static_cast<bool>(s) << '\n'; // false
}#include <cstdio>
#include <memory>
struct FileCloser {
void operator()(std::FILE* f) const { if (f) std::fclose(f); }
};
int main() {
std::unique_ptr<std::FILE, FileCloser> f(std::fopen("/dev/null", "r"));
(void)f;
}Common Gotchas
Copying a `unique_ptr`
does not compile. `std::move` if you mean transfer.
`get()` and then `delete`
the `unique_ptr` still owns the pointer and will `delete` again. Use `release` only if you really take over, or do not `delete` a `get()` pointer.
`unique_ptr<T>` with `new T[]`
wrong deleter. Use `unique_ptr<T[]>` or `vector`.
Converting `unique_ptr<Derived>` to `unique_ptr<Base>`
works if the default deleter is compatible; custom deleters need to match.