On this page
  1. std::make_unique
  2. Overview
  3. Notes & Pitfalls
  4. Example

std::make_unique

Prev: std::unique_ptr | Up: memory | Next: std::shared_ptr
Rel: std::unique_ptr, new & delete
Ref: cppreference — std::make_unique
Tags: #public #prog #cpp #stdlib #memory #tier/core


Overview

std::make_unique<T>(args...) (<memory>, C++14) allocates and constructs a T and returns it wrapped in a std::unique_ptr<T> without repeating the type name or writing raw new.

Notes & Pitfalls

Example

#include <cassert>
#include <memory>
#include <string>

int main() {
    auto s = std::make_unique<std::string>("owned");
    assert(*s == "owned");
}

Linked from

Related notes