blob: ae3b325ac76912f5475751a6d55b430b61138e83 [file] [log] [blame]
Brian Silverman355f11d2018-08-04 23:57:00 -07001////
2Copyright 2017 Peter Dimov
3Copyright 2017 Glen Joseph Fernandes (glenjofe@gmail.com)
4
5Distributed under the Boost Software License, Version 1.0.
6
7See accompanying file LICENSE_1_0.txt or copy at
8http://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
19The `make_unique` function templates provide convenient and safe ways to
20create `std::unique_ptr` objects.
21
22## Rationale
23
24The {cpp}11 standard introduced `std::unique_ptr` but did not provide any
25`make_unique` utility like `std::make_shared` that provided the same
26exception safety and facility to avoid writing `new` expressions. Before it
27was implemented by some standard library vendors (and prior to the {cpp}14
28standard introducing `std::make_unique`), this library provided it due to
29requests from users.
30
31This library also provides additional overloads of `make_unique` for
32default-initialization, when users do not need or want to incur the expense
33of value-initialization. The {cpp} standard does not yet provide this
34feature 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```
42namespace 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```
68template<class T, class... Args>
69 std::unique_ptr<T> make_unique(Args&&... args);
70```
71::
72Remarks::: These overloads shall only participate in overload resolution when
73`T` is not an array type.
74Returns::: `std::unique_ptr<T>(new T(std::forward<Args>(args)\...)`.
75Example::: `auto p = make_unique<int>();`
76
77```
78template<class T>
79 std::unique_ptr<T> make_unique(remove_reference_t<T>&& v);
80```
81::
82Remarks::: These overloads shall only participate in overload resolution when
83`T` is not an array type.
84Returns::: `std::unique_ptr<T>(new T(std::move(v))`.
85Example::: `auto p = make_unique<std::vector<int> >({1, 2});`
86
87```
88template<class T>
89 std::unique_ptr<T> make_unique(std::size_t n);
90```
91::
92Remarks::: These overloads shall only participate in overload resolution when
93`T` is an array type of the form `U[]`.
94Returns::: `std::unique_ptr<U[]>(new U[n]())`.
95Example::: `auto p = make_unique<double[]>(1024);`
96
97```
98template<class T>
99 std::unique_ptr<T> make_unique_noinit();
100```
101::
102Remarks::: These overloads shall only participate in overload resolution when
103`T` is not an array type.
104Returns::: `std::unique_ptr<T>(new T)`.
105Example::: `auto p = make_unique_noinit<double[1024]>();`
106
107```
108template<class T>
109 std::unique_ptr<T> make_unique_noinit(std::size_t n);
110```
111::
112Remarks::: These overloads shall only participate in overload resolution when
113`T` is an array type of the form `U[]`.
114Returns::: `std::unique_ptr<U[]>(new U[n])`.
115Example::: `auto p = make_unique_noinit<double[]>(1024);`