Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

C++ std::make_unique and std::make_shared

Reference for std::make_unique, std::make_shared, allocate_shared and the for_overwrite variants: why to prefer them to new, exception safety and the limits.

std::make_unique<T>(args...) and std::make_shared<T>(args...) construct an object on the heap and return it already owned by a smart pointer. They replace most uses of new. make_unique was added in C++14, make_shared in C++11, and both live in <memory>.

They remove the type name repetition, rule out leaks between allocation and ownership, and (for make_shared) merge two allocations into one.

C++
auto u = std::make_unique<Widget>(1, "a");
auto s = std::make_shared<Widget>(2, "b");

Try it

C++ Factories and arrays

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Aggregates, braced lists and allocate_shared

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args);     // C++14
template<class T> unique_ptr<T> make_unique(size_t n);                          // T = U[]
template<class T> unique_ptr<T> make_unique_for_overwrite();                    // C++20

template<class T, class... Args> shared_ptr<T> make_shared(Args&&... args);     // C++11
template<class T> shared_ptr<T> make_shared(size_t n);                          // C++20, T = U[]
template<class T> shared_ptr<T> make_shared_for_overwrite();                    // C++20
template<class T, class A, class... Args> shared_ptr<T> allocate_shared(const A& a, Args&&... args);
Name Description
args Forwarded to the constructor of T with perfect forwarding.
n Element count for array forms (value-initialized elements).
a Allocator used by allocate_shared for object and control block.

Returns: the owning smart pointer. Errors: throws std::bad_alloc if memory is exhausted, or whatever the constructor throws; nothing leaks in either case.

Usage notes

  • Exception safety: f(std::unique_ptr<A>(new A), g()) could leak in C++14 if g() threw after new A but before the pointer was built. make_unique removes that risk (C++17 also tightened evaluation order).
  • Performance: make_shared allocates the object and the control block together. The downside is that memory of the object is held until the last weak_ptr goes away.
  • No braced-init forwarding: make_unique<std::vector<int>>({1, 2, 3}) does not compile. Pass an explicit std::vector<int>{1, 2, 3}.
  • Aggregates: since C++20, parentheses initialization of aggregates works, so make_unique<Point>(1, 2) is valid.
  • Custom deleters cannot be passed to the make functions. Construct the smart pointer yourself, see Custom deleters.
  • Private constructors are inaccessible to the make functions, since they construct from outside the class.
  • make_unique for arrays value-initializes (zeroes) the elements, which costs time for big buffers. make_unique_for_overwrite skips that and leaves values indeterminate, so write before reading.
  • Bounded arrays (make_unique<int[5]>) are deleted functions.
  • Overloaded operator new at class level is bypassed by make_shared. Use allocate_shared if you need control.