1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
4 <title>make_unique
</title>
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=iso-8859-1">
7 <body text=
"#000000" bgcolor=
"#ffffff" link=
"#0000ff" vlink=
"#0000ff">
8 <h1><img height=
"86" alt=
"boost.png (6897 bytes)" src=
"../../boost.png"
9 width=
"277" align=
"middle" border=
"0">make_unique
</h1>
10 <p><a href=
"#introduction">Introduction
</a><br>
11 <a href=
"#synopsis">Synopsis
</a><br>
12 <a href=
"#common">Common Requirements
</a><br>
13 <a href=
"#functions">Free Functions
</a><br>
14 <a href=
"#history">History
</a></p>
15 <h2><a name=
"introduction">Introduction
</a></h2>
16 <p>The header file
<boost/make_unique.hpp
> provides overloaded
17 function template
<code>make_unique
</code> for convenient creation of
18 <code>unique_ptr
</code> objects.
</p>
19 <h2><a name=
"synopsis">Synopsis
</a></h2>
20 <pre>namespace boost {
21 template
<class U
> // U is not array
22 unique_ptr
<U
> <a href=
"#functions">make_unique
</a>();
24 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
25 template
<class U, class... Args
> // U is not array
26 unique_ptr
<U
> <a href=
"#functions">make_unique
</a>(Args
&&... args);
29 template
<class U
> // U is not array
30 unique_ptr
<U
> <a href=
"#functions">make_unique
</a>(U
&& value);
32 template
<class U
> // U is T[]
33 unique_ptr
<U
> <a href=
"#functions">make_unique
</a>(size_t size);
35 template
<class U
> // U is not array
36 unique_ptr
<U
> <a href=
"#functions">make_unique_noinit
</a>();
38 template
<class U
> // U is T[]
39 unique_ptr
<U
> <a href=
"#functions">make_unique_noinit
</a>(size_t size);
41 <h2><a name=
"common">Common Requirements
</a></h2>
42 <pre>template
<class U
>
43 unique_ptr
<U
> make_unique(
<em>args
</em>);
44 template
<class U
>
45 unique_ptr
<U
> make_unique_noinit(
<em>args
</em>);
</pre>
47 <p><b>Effects:
</b> Allocates memory for an object of type
<code>U
</code>
48 (or
<code>T[size]
</code> when
<code>U
</code> is
<code>T[]
</code>,
49 where
<code>size
</code> is determined from
<code>args
</code> as
50 specified by the concrete overload). The object is initialized from
51 <code>args
</code> as specified by the concrete overload. If an
52 exception is thrown, the functions have no effect.
</p>
53 <p><b>Returns:
</b> A
<code>unique_ptr
</code> instance that stores and
54 owns the address of the newly constructed object.
</p>
55 <p><b>Postconditions:
</b> <code>r.get() !=
0</code>, where
56 <code>r
</code> is the return value.
</p>
57 <p><b>Throws:
</b> <code>bad_alloc
</code>, or an exception thrown from
58 the initialization of the object.
</p>
59 <p><b>Remarks:
</b></p>
61 <p>When an object of a non-array type
<code>T
</code> is specified to
62 be initialized to a value
<code>value
</code>, or to
63 <code>T(list...)
</code>, where
<code>list...
</code> is a list of
64 constructor arguments,
<code>make_unique
</code> shall perform this
65 initialization via the expression
<code>new T(value)
</code> or
66 <code>new T(list...)
</code> respectively.
</p>
67 <p>When an object of type
<code>T
</code> is specified to be
68 value-initialized,
<code>make_unique
</code> shall perform this
69 initialization via the expression
<code>new T()
</code>.
</p>
70 <p>When an object of type
<code>T
</code> is specified to be
71 default-initialized,
<code>make_unique_noinit
</code> shall perform
72 this initialization via the expression
<code>new T
</code>.
</p>
75 <h2><a name=
"functions">Free Functions
</a></h2>
76 <pre>template
<class U, class... Args
>
77 unique_ptr
<U
> make_unique(Args
&&... args);
</pre>
79 <p><b>Returns:
</b> A unique_ptr to an object of type
<code>U
</code>,
80 initialized to
<code>U(forward
<Args
>(args)...)
</code>.
</p>
81 <p><b>Remarks:
</b> This overload shall only participate in overload
82 resolution when
<code>U
</code> is not an array type.
</p>
83 <p><b>Examples:
</b></p>
85 <pre>unique_ptr
<float
> p1 = boost::make_unique
<float
>();
86 unique_ptr
<point
> p2 = boost::make_unique
<point
>(x, y);
</pre>
89 <pre>template
<class U
>
90 unique_ptr
<U
> make_unique(U
&& value);
</pre>
92 <p><b>Returns:
</b> A unique_ptr to an object of type
<code>U
</code>,
93 initialized to
<code>move(value)
</code>.
</p>
94 <p><b>Remarks:
</b> This overload shall only participate in overload
95 resolution when
<code>U
</code> is not an array type.
</p>
96 <p><b>Examples:
</b></p>
98 <pre>unique_ptr
<string
> p1 = boost::make_unique
<string
>({'a', 'b'});
99 unique_ptr
<point
> p2 = boost::make_unique
<point
>({-
10,
25});
</pre>
102 <pre>template
<class U
>
103 unique_ptr
<U
> make_unique(size_t size);
</pre>
105 <p><b>Returns:
</b> A unique_ptr to a value-initialized object of type
106 <code>T[size]
</code>.
</p>
107 <p><b>Remarks:
</b> This overload shall only participate in overload
108 resolution when
<code>U
</code> is of the form
<code>T[]
</code>.
</p>
109 <p><b>Examples:
</b></p>
111 <pre>unique_ptr
<double[]
> p1 = boost::make_unique
<double[]
>(
4);
112 unique_ptr
<int[][
2]
> p2 = boost::make_unique
<int[][
2]
>(
2);
</pre>
115 <pre>template
<class U
>
116 unique_ptr
<U
> make_unique_noinit();
</pre>
118 <p><b>Returns:
</b> A unique_ptr to a default-initialized object of
119 type
<code>U
</code>.
</p>
120 <p><b>Remarks:
</b> This overload shall only participate in overload
121 resolution when
<code>U
</code> is not an array type.
</p>
122 <p><b>Examples:
</b></p>
124 <pre>unique_ptr
<float
> p1 = boost::make_unique_noinit
<float
>();
125 unique_ptr
<point
> p2 = boost::make_unique_noinit
<point
>();
</pre>
128 <pre>template
<class U
>
129 unique_ptr
<U
> make_unique_noinit(size_t size);
</pre>
131 <p><b>Returns:
</b> A unique_ptr to a default-initialized object of
132 type
<code>T[size]
</code>.
</p>
133 <p><b>Remarks:
</b> This overload shall only participate in overload
134 resolution when
<code>U
</code> is of the form
<code>T[]
</code>.
</p>
135 <p><b>Examples:
</b></p>
137 <pre>unique_ptr
<double[]
> p1 = boost::make_unique_noinit
<double[]
>(
4);
138 unique_ptr
<int[][
2]
> p2 = boost::make_unique_noinit
<int[][
2]
>(
2);
</pre>
141 <h2><a name=
"history">History
</a></h2>
142 <p>January
2014. Glen Fernandes contributed implementations of
143 make_unique for objects and arrays.
</p>
146 <p><small>Copyright
2012-
2014 Glen Fernandes. Distributed under the
147 Boost Software License, Version
1.0. See accompanying file
148 <a href=
"../../LICENSE_1_0.txt">LICENSE_1_0.txt
</a> or copy at
149 <a href=
"http://www.boost.org/LICENSE_1_0.txt">
150 http://www.boost.org/LICENSE_1_0.txt
</a>.
</small></p>