Brian Silverman | 355f11d | 2018-08-04 23:57:00 -0700 | [diff] [blame^] | 1 | //// |
| 2 | Copyright 2017 Peter Dimov |
| 3 | Copyright 2017 Glen Joseph Fernandes (glenjofe@gmail.com) |
| 4 | |
| 5 | Distributed under the Boost Software License, Version 1.0. |
| 6 | |
| 7 | See accompanying file LICENSE_1_0.txt or copy at |
| 8 | http://www.boost.org/LICENSE_1_0.txt |
| 9 | //// |
| 10 | |
| 11 | [#make_unique] |
| 12 | # make_unique: Creating unique_ptr |
| 13 | :toc: |
| 14 | :toc-title: |
| 15 | :idprefix: make_unique_ |
| 16 | |
| 17 | ## Description |
| 18 | |
| 19 | The `make_unique` function templates provide convenient and safe ways to |
| 20 | create `std::unique_ptr` objects. |
| 21 | |
| 22 | ## Rationale |
| 23 | |
| 24 | The {cpp}11 standard introduced `std::unique_ptr` but did not provide any |
| 25 | `make_unique` utility like `std::make_shared` that provided the same |
| 26 | exception safety and facility to avoid writing `new` expressions. Before it |
| 27 | was implemented by some standard library vendors (and prior to the {cpp}14 |
| 28 | standard introducing `std::make_unique`), this library provided it due to |
| 29 | requests from users. |
| 30 | |
| 31 | This library also provides additional overloads of `make_unique` for |
| 32 | default-initialization, when users do not need or want to incur the expense |
| 33 | of value-initialization. The {cpp} standard does not yet provide this |
| 34 | feature with `std::make_unique`. |
| 35 | |
| 36 | ## Synopsis |
| 37 | |
| 38 | `make_unique` is defined in `<boost/smart_ptr/make_unique.hpp>`. |
| 39 | |
| 40 | [subs=+quotes] |
| 41 | ``` |
| 42 | namespace boost { |
| 43 | `// only if T is not an array type` |
| 44 | template<class T, class... Args> |
| 45 | std::unique_ptr<T> make_unique(Args&&... args); |
| 46 | |
| 47 | `// only if T is not an array type` |
| 48 | template<class T> |
| 49 | std::unique_ptr<T> make_unique(remove_reference_t<T>&& v); |
| 50 | |
| 51 | `// only if T is an array type of the form U[]` |
| 52 | template<class T> |
| 53 | std::unique_ptr<T> make_unique(std::size_t n); |
| 54 | |
| 55 | `// only if T is not an array type` |
| 56 | template<class T> |
| 57 | std::unique_ptr<T> make_unique_noinit(); |
| 58 | |
| 59 | `// only if T is an array type of the form U[]` |
| 60 | template<class T> |
| 61 | std::unique_ptr<T> make_unique_noinit(std::size_t n); |
| 62 | } |
| 63 | ``` |
| 64 | |
| 65 | ## Free Functions |
| 66 | |
| 67 | ``` |
| 68 | template<class T, class... Args> |
| 69 | std::unique_ptr<T> make_unique(Args&&... args); |
| 70 | ``` |
| 71 | :: |
| 72 | Remarks::: These overloads shall only participate in overload resolution when |
| 73 | `T` is not an array type. |
| 74 | Returns::: `std::unique_ptr<T>(new T(std::forward<Args>(args)\...)`. |
| 75 | Example::: `auto p = make_unique<int>();` |
| 76 | |
| 77 | ``` |
| 78 | template<class T> |
| 79 | std::unique_ptr<T> make_unique(remove_reference_t<T>&& v); |
| 80 | ``` |
| 81 | :: |
| 82 | Remarks::: These overloads shall only participate in overload resolution when |
| 83 | `T` is not an array type. |
| 84 | Returns::: `std::unique_ptr<T>(new T(std::move(v))`. |
| 85 | Example::: `auto p = make_unique<std::vector<int> >({1, 2});` |
| 86 | |
| 87 | ``` |
| 88 | template<class T> |
| 89 | std::unique_ptr<T> make_unique(std::size_t n); |
| 90 | ``` |
| 91 | :: |
| 92 | Remarks::: These overloads shall only participate in overload resolution when |
| 93 | `T` is an array type of the form `U[]`. |
| 94 | Returns::: `std::unique_ptr<U[]>(new U[n]())`. |
| 95 | Example::: `auto p = make_unique<double[]>(1024);` |
| 96 | |
| 97 | ``` |
| 98 | template<class T> |
| 99 | std::unique_ptr<T> make_unique_noinit(); |
| 100 | ``` |
| 101 | :: |
| 102 | Remarks::: These overloads shall only participate in overload resolution when |
| 103 | `T` is not an array type. |
| 104 | Returns::: `std::unique_ptr<T>(new T)`. |
| 105 | Example::: `auto p = make_unique_noinit<double[1024]>();` |
| 106 | |
| 107 | ``` |
| 108 | template<class T> |
| 109 | std::unique_ptr<T> make_unique_noinit(std::size_t n); |
| 110 | ``` |
| 111 | :: |
| 112 | Remarks::: These overloads shall only participate in overload resolution when |
| 113 | `T` is an array type of the form `U[]`. |
| 114 | Returns::: `std::unique_ptr<U[]>(new U[n])`. |
| 115 | Example::: `auto p = make_unique_noinit<double[]>(1024);` |