On this page
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
- Always prefer
std::make_unique<T>(args...)over rawnew: Prevents exception-safety leaks in multi-argument function calls and eliminates repeating the type name.
Example
#include <cassert>
#include <memory>
#include <string>
int main() {
auto s = std::make_unique<std::string>("owned");
assert(*s == "owned");
}
Linked from
Related notes
- tuts/cpp/stdlib/memory/shared_ptr Linked here; links to this page; shared tags: #cpp, #memory, #prog, #stdlib, #tier
- tuts/cpp/stdlib/memory/unique_ptr Linked here; links to this page; shared tags: #cpp, #memory, #prog, #stdlib, #tier
- tuts/cpp/builtins/new_and_delete Linked here; links to this page; shared tags: #cpp, #memory, #prog, #tier
- tuts/cpp/stdlib/memory Linked here; links to this page; shared tags: #cpp, #memory, #prog, #stdlib
- tuts/cpp/stdlib/memory/auto_ptr links to this page; shared tags: #cpp, #memory, #prog, #stdlib, #tier
- tuts/cpp/stdlib/memory/weak_ptr shared tags: #cpp, #memory, #prog, #stdlib, #tier
- tuts/cpp/builtins/sizeof_and_alignof shared tags: #cpp, #memory, #prog, #tier
- tuts/cpp/language_and_raii/raii_smart_pointers_and_lifetime shared tags: #cpp, #memory, #prog, #tier
- tuts/cpp/stdlib/algorithm_and_ranges/find_if shared tags: #cpp, #prog, #stdlib, #tier
- tuts/cpp/stdlib/algorithm_and_ranges/ranges_views shared tags: #cpp, #prog, #stdlib, #tier