std::unique_ptr
Creating (<memory>)
auto p = std::make_unique<Account>("Asha", 1000); // ✅ preferred (C++14)
std::unique_ptr<int[]> arr = std::make_unique<int[]>(10);
std::unique_ptr<Account> empty; // nullptr
Using
p->deposit(500); // like a raw pointer
(*p).balance();
if (p) { ... } // explicit operator bool
p.get(); // raw pointer (non-owning) for APIs that need one
Unique ownership
- Exactly one unique_ptr owns the object
- When the owner is destroyed (scope ends, reset, reassigned) → the object is deleted automatically
- Cannot be copied ❌, only moved ✅:
auto q = std::move(p); // q owns it now; p is nullptr
Useful members
| Member | Effect |
|---|---|
reset() | delete the object now; become null |
reset(new T) | replace the owned object |
release() | give up ownership without deleting (returns raw pointer — you must delete it!) |
get() | raw pointer without giving up ownership |
Ownership in function signatures ✅
| Signature | Meaning |
|---|---|
void f(std::unique_ptr<T> p) | f takes ownership (caller must std::move) |
void f(T& obj) / const T& | f just uses the object |
void f(T* obj) | f uses it, may be null; doesn't own |
std::unique_ptr<T> make() | returns ownership to the caller |
Polymorphism
std::vector<std::unique_ptr<Shape>> shapes;
shapes.push_back(std::make_unique<Circle>(2.0));
The base needs a virtual destructor.
Cost
Same size as a raw pointer (with the default deleter) and no runtime overhead — "zero-cost abstraction".
Example
#include <iostream>
#include <memory>
#include <string>
#include <vector>
class Account {
public:
Account(std::string owner, double balance) : owner_(std::move(owner)), balance_(balance) {
std::cout << " + " << owner_ << '\n';
}
~Account() { std::cout << " - " << owner_ << '\n'; }
void deposit(double x) { balance_ += x; }
double balance() const { return balance_; }
const std::string& owner() const { return owner_; }
private:
std::string owner_;
double balance_;
};
std::unique_ptr<Account> open_account(const std::string& owner) { // factory returns ownership
return std::make_unique<Account>(owner, 500);
}
void print(const Account& a) { std::cout << " " << a.owner() << ": " << a.balance() << '\n'; } // just uses it
void archive(std::unique_ptr<Account> acc) { // takes ownership
std::cout << " archiving " << acc->owner() << '\n';
} // deleted here
int main() {
std::cout << "create:\n";
auto asha = std::make_unique<Account>("Asha", 1000);
asha->deposit(250);
print(*asha);
std::cout << "move ownership:\n";
std::unique_ptr<Account> owner2 = std::move(asha); // copying would not compile
std::cout << " asha is " << (asha ? "valid" : "empty") << ", owner2 has " << owner2->owner() << '\n';
std::cout << "factory + archive:\n";
auto ravi = open_account("Ravi");
archive(std::move(ravi));
std::cout << " after archive, ravi is " << (ravi ? "valid" : "empty") << '\n';
std::cout << "reset:\n";
owner2.reset(); // delete now
std::cout << "vector of unique_ptr:\n";
std::vector<std::unique_ptr<Account>> bank;
bank.push_back(std::make_unique<Account>("Sita", 3000));
bank.push_back(open_account("Hari"));
for (const auto& a : bank) print(*a);
std::cout << "sizeof(unique_ptr) = " << sizeof(std::unique_ptr<Account>)
<< ", sizeof(raw pointer) = " << sizeof(Account*) << '\n';
std::cout << "end of main:\n";
return 0;
}
/* Output:
create:
+ Asha
Asha: 1250
move ownership:
asha is empty, owner2 has Asha
factory + archive:
+ Ravi
archiving Ravi
- Ravi
after archive, ravi is empty
reset:
- Asha
vector of unique_ptr:
+ Sita
+ Hari
Sita: 3000
Hari: 500
sizeof(unique_ptr) = 8, sizeof(raw pointer) = 8
end of main:
- Sita
- Hari
*/