//// Copyright 2017 Peter Dimov Copyright 2017 Glen Joseph Fernandes (glenjofe@gmail.com) Distributed under the Boost Software License, Version 1.0. See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt //// [#make_unique] # make_unique: Creating unique_ptr :toc: :toc-title: :idprefix: make_unique_ ## Description The `make_unique` function templates provide convenient and safe ways to create `std::unique_ptr` objects. ## Rationale The {cpp}11 standard introduced `std::unique_ptr` but did not provide any `make_unique` utility like `std::make_shared` that provided the same exception safety and facility to avoid writing `new` expressions. Before it was implemented by some standard library vendors (and prior to the {cpp}14 standard introducing `std::make_unique`), this library provided it due to requests from users. This library also provides additional overloads of `make_unique` for default-initialization, when users do not need or want to incur the expense of value-initialization. The {cpp} standard does not yet provide this feature with `std::make_unique`. ## Synopsis `make_unique` is defined in ``. [subs=+quotes] ``` namespace boost { `// only if T is not an array type` template std::unique_ptr make_unique(Args&&... args); `// only if T is not an array type` template std::unique_ptr make_unique(remove_reference_t&& v); `// only if T is an array type of the form U[]` template std::unique_ptr make_unique(std::size_t n); `// only if T is not an array type` template std::unique_ptr make_unique_noinit(); `// only if T is an array type of the form U[]` template std::unique_ptr make_unique_noinit(std::size_t n); } ``` ## Free Functions ``` template std::unique_ptr make_unique(Args&&... args); ``` [none] * {blank} + Remarks:: These overloads shall only participate in overload resolution when `T` is not an array type. Returns:: `std::unique_ptr(new T(std::forward(args)\...)`. Example:: `auto p = make_unique();` ``` template std::unique_ptr make_unique(remove_reference_t&& v); ``` [none] * {blank} + Remarks:: These overloads shall only participate in overload resolution when `T` is not an array type. Returns:: `std::unique_ptr(new T(std::move(v))`. Example:: `auto p = make_unique >({1, 2});` ``` template std::unique_ptr make_unique(std::size_t n); ``` [none] * {blank} + Remarks:: These overloads shall only participate in overload resolution when `T` is an array type of the form `U[]`. Returns:: `std::unique_ptr(new U[n]())`. Example:: `auto p = make_unique(1024);` ``` template std::unique_ptr make_unique_noinit(); ``` [none] * {blank} + Remarks:: These overloads shall only participate in overload resolution when `T` is not an array type. Returns:: `std::unique_ptr(new T)`. Example:: `auto p = make_unique_noinit();` ``` template std::unique_ptr make_unique_noinit(std::size_t n); ``` [none] * {blank} + Remarks:: These overloads shall only participate in overload resolution when `T` is an array type of the form `U[]`. Returns:: `std::unique_ptr(new U[n])`. Example:: `auto p = make_unique_noinit(1024);`