Skip to content

std::make_unique (C++14) ​

In a Nutshell ​

Safely creates a std::unique_ptr — safer than writing new directly, and more concise.

#include <memory>

Core API Cheat Sheet ​

OperationSignatureDescription
Construct objecttemplate<class T, class...Args> unique_ptr<T> make_unique(Args&&... args)Creates a unique_ptr for a non-array type (C++14)
Construct arraytemplate<class T> unique_ptr<T> make_unique(std::size_t size)Creates an array of unknown bound, elements are value-initialized (C++14)
Fixed-length arrays deletedtemplate<class T, class...Args> /* unspecified */ make_unique(Args&&... args) = deleteArrays of known bound are explicitly deleted (C++14)
Default-initialize objecttemplate<class T> unique_ptr<T> make_unique_for_overwrite()Creates a non-array type, default-initialized (C++20)
Default-initialize arraytemplate<class T> unique_ptr<T> make_unique_for_overwrite(std::size_t size)Creates an array of unknown bound, default-initialized (C++20)

Minimal Example ​

C++
#include <memory>
#include <cstdio>
// Standard: C++14
struct Foo {
    Foo(int v) : val(v) { std::printf("Foo(%d)\n", val); }
    ~Foo() { std::printf("~Foo()\n"); }
    int val;
};
int main() {
    auto p1 = std::make_unique<Foo>(42);
    auto p2 = std::make_unique<Foo[]>(3);
}

Embedded Applicability: High ​

  • A zero-overhead abstraction: after compilation it is exactly equivalent to using new directly
  • Expresses exclusive ownership semantics explicitly, preventing resource leaks
  • Avoids the exception-safety hazard caused by separating the new expression from the unique_ptr construction
  • Available since C++14, supported by all mainstream embedded compilers

Compiler Support ​

GCCClangMSVC
TBATBATBA

See Also ​


Part of the content references cppreference.com, licensed under CC-BY-SA 4.0

pdf-latest-4-g85128cc · 85128cc · 2026-10-05