Skip to content

std::make_unique (C++14)

In a Nutshell

Safely creates std::unique_ptr, offering better safety and more concise code than writing new directly.

#include <memory>

Core API Quick Reference

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 with value initialization (C++14)
Fixed-length array forbiddentemplate<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 with default initialization (C++20)
Default-initialize arraytemplate<class T> unique_ptr<T> make_unique_for_overwrite(std::size_t size)Creates an array of unknown bound with default initialization (C++20)

Minimal Example

cpp
#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; compiles to code completely equivalent to directly using new
  • Explicitly expresses exclusive ownership semantics, preventing resource leaks
  • Avoids the exception safety hazard caused by separating the new expression from the unique_ptr constructor
  • Available since C++14, supported by all mainstream embedded compilers

Compiler Support

GCCClangMSVC
TBATBATBA

See Also


Some content referenced from cppreference.com, licensed under CC-BY-SA 4.0

Built with VitePress