• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1////
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  `// T is not an array`
44  template<class T, class... Args>
45    std::unique_ptr<T> make_unique(Args&&... args);
46
47  `// T is not an array`
48  template<class T>
49    std::unique_ptr<T> make_unique(type_identity_t<T>&& v);
50
51  `// T is an array of unknown bounds`
52  template<class T>
53    std::unique_ptr<T> make_unique(std::size_t n);
54
55  `// T is not an array`
56  template<class T>
57    std::unique_ptr<T> make_unique_noinit();
58
59  `// T is an array of unknown bounds`
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[none]
72* {blank}
73+
74Constraints:: `T` is not an array.
75Returns:: `std::unique_ptr<T>(new T(std::forward<Args>(args)\...)`.
76Example:: `auto p = make_unique<int>();`
77
78```
79template<class T>
80  std::unique_ptr<T> make_unique(type_identity_t<T>&& v);
81```
82[none]
83* {blank}
84+
85Constraints:: `T` is not an array.
86Returns:: `std::unique_ptr<T>(new T(std::move(v))`.
87Example:: `auto p = make_unique<std::vector<int> >({1, 2});`
88
89```
90template<class T>
91  std::unique_ptr<T> make_unique(std::size_t n);
92```
93[none]
94* {blank}
95+
96Constraints:: `T` is an array of unknown bounds.
97Returns:: `std::unique_ptr<T>(new remove_extent_t<T>[n]())`.
98Example:: `auto p = make_unique<double[]>(1024);`
99
100```
101template<class T>
102  std::unique_ptr<T> make_unique_noinit();
103```
104[none]
105* {blank}
106+
107Constraints:: `T` is not an array.
108Returns:: `std::unique_ptr<T>(new T)`.
109Example:: `auto p = make_unique_noinit<std::array<double, 1024> >();`
110
111```
112template<class T>
113  std::unique_ptr<T> make_unique_noinit(std::size_t n);
114```
115[none]
116* {blank}
117+
118Constraints:: `T` is an array of unknown bounds.
119Returns:: `std::unique_ptr<T>(new remove_extent_t<T>[n])`.
120Example:: `auto p = make_unique_noinit<double[]>(1024);`
121