mirror of
https://github.com/boostorg/smart_ptr.git
synced 2026-01-28 18:12:28 +01:00
125 lines
5.8 KiB
HTML
125 lines
5.8 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<title>make_unique</title>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
</head>
|
|
<body text="#000000" bgcolor="#ffffff" link="#0000ff" vlink="#0000ff">
|
|
<h1><img height="86" alt="boost.png (6897 bytes)" src="../../boost.png"
|
|
width="277" align="middle" border="0">make_unique</h1>
|
|
<p><a href="#Introduction">Introduction</a><br>
|
|
<a href="#Synopsis">Synopsis</a><br>
|
|
<a href="#functions">Free Functions</a><br>
|
|
<a href="#example">Examples</a><br>
|
|
<a href="#history">History</a></p>
|
|
<h2><a name="Introduction">Introduction</a></h2>
|
|
<p>The header file <boost/make_unique.hpp> provides overloaded
|
|
function template <code>make_unique</code> for convenient creation of
|
|
<code>unique_ptr</code> objects.</p>
|
|
<h2><a name="Synopsis">Synopsis</a></h2>
|
|
<pre>namespace boost {
|
|
template<typename U> // U is not array
|
|
unique_ptr<U> <a href="#functions">make_unique</a>();
|
|
|
|
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
|
|
template<typename U, typename... Args> // U is not array
|
|
unique_ptr<U> <a href="#functions">make_unique</a>(Args&&... args);
|
|
#endif
|
|
|
|
template<typename U> // U is not array
|
|
unique_ptr<U> <a href="#functions">make_unique_noinit</a>();
|
|
|
|
template<typename U> // U is T[]
|
|
unique_ptr<U> <a href="#functions">make_unique</a>(size_t size);
|
|
|
|
template<typename U> // U is T[]
|
|
unique_ptr<U> <a href="#functions">make_unique_noinit</a>(size_t size);
|
|
}</pre>
|
|
<h2><a name="functions">Free Functions</a></h2>
|
|
<pre>template<typename U> // U is not array
|
|
unique_ptr<U> make_unique();</pre>
|
|
<blockquote>
|
|
<p><b>Requires:</b> The expression <code>new U()</code> shall be
|
|
well-formed.</p>
|
|
<p><b>Effects:</b> Constructs an object of type <code>U</code> via the
|
|
expression <code>new U()</code>.</p>
|
|
<p><b>Returns:</b> A <code>unique_ptr</code> instance that stores and
|
|
owns the address of the newly constructed object.</p>
|
|
<p><b>Postconditions:</b> <code>get() != 0</code>.</p>
|
|
<p><b>Throws:</b> <code>bad_alloc</code>, or an exception thrown from
|
|
the constructor of <code>U</code>.</p>
|
|
</blockquote>
|
|
<pre>template<typename U, typename... Args> // U is not array
|
|
unique_ptr<U> make_unique(Args&&... args);</pre>
|
|
<blockquote>
|
|
<p><b>Requires:</b> The expression
|
|
<code>new U(forward<Args>(args)...)</code> shall be
|
|
well-formed.</p>
|
|
<p><b>Effects:</b> Constructs an object of type <code>U</code> via the
|
|
expression <code>new U(forward<Args>(args)...)</code>.</p>
|
|
</blockquote>
|
|
<pre>template<typename U> // U is not array
|
|
unique_ptr<U> make_unique_noinit();</pre>
|
|
<blockquote>
|
|
<p><b>Requires:</b> The expression <code>new U</code> shall be
|
|
well-formed.</p>
|
|
<p><b>Effects:</b> Constructs an object of type <code>U</code> via the
|
|
expression <code>new U</code>.</p>
|
|
</blockquote>
|
|
<pre>template<typename U> // U is T[]
|
|
unique_ptr<U> make_unique(size_t size);</pre>
|
|
<blockquote>
|
|
<p><b>Requires:</b> The expression <code>new T[size]()</code> shall be
|
|
well-formed.</p>
|
|
<p><b>Effects:</b> Constructs an array of objects of type
|
|
<code>U</code> and size <code>size</code> via the expression
|
|
<code>new T[size]()</code>.</p>
|
|
</blockquote>
|
|
<pre>template<typename U> // U is T[]
|
|
unique_ptr<U> make_unique_noinit(size_t size);</pre>
|
|
<blockquote>
|
|
<p><b>Requires:</b> The expression <code>new T[size]</code> shall be
|
|
well-formed.</p>
|
|
<p><b>Effects:</b> Constructs an array of objects of type
|
|
<code>U</code> and size <code>size</code> via the expression
|
|
<code>new T[size]</code>.</p>
|
|
</blockquote>
|
|
<h2><a name="example">Examples</a></h2>
|
|
<p>For objects with value-initialization.</p>
|
|
<blockquote>
|
|
<pre>std::unique_ptr<float> p1 = boost::make_unique<float>();
|
|
std::unique_ptr<point> p2 = boost::make_unique<point>();</pre>
|
|
</blockquote>
|
|
<p>For objects with construction arguments.</p>
|
|
<blockquote>
|
|
<pre>std::unique_ptr<float> p3 = boost::make_unique<float>(1.5);
|
|
std::unique_ptr<point> p4 = boost::make_unique<point>(x, y);</pre>
|
|
</blockquote>
|
|
<p>For objects with default-initialization.</p>
|
|
<blockquote>
|
|
<pre>std::unique_ptr<float> p4 = boost::make_unique_noinit<float>();
|
|
std::unique_ptr<point> p5 = boost::make_unique_noinit<point>();</pre>
|
|
</blockquote>
|
|
<p>For arrays with value-initialization.</p>
|
|
<blockquote>
|
|
<pre>std::unique_ptr<double[]> a1 = boost::make_unique<double[]>();
|
|
std::unique_ptr<int[][4]> a2 = boost::make_unique<int[][4]>();</pre>
|
|
</blockquote>
|
|
<p>For arrays with default-initialization.</p>
|
|
<blockquote>
|
|
<pre>std::unique_ptr<double[]> a3 = boost::make_unique_noinit<double[]>();
|
|
std::unique_ptr<int[][4]> a4 = boost::make_unique_noinit<int[][4]>();</pre>
|
|
</blockquote>
|
|
<h2><a name="history">History</a></h2>
|
|
<p>January 2014. Glen Fernandes contributed implementations of
|
|
make_unique for objects and arrays.</p>
|
|
<hr>
|
|
<p>$Date: 2014-01-20 11:10:00 -0800 (Mon, 20 Jan 2014) $</p>
|
|
<p><small>Copyright 2012-2014 Glen Fernandes. Distributed under the
|
|
Boost Software License, Version 1.0. See accompanying file
|
|
<a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy at
|
|
<a href="http://www.boost.org/LICENSE_1_0.txt">
|
|
http://www.boost.org/LICENSE_1_0.txt</a>.</small></p>
|
|
</body>
|
|
</html>
|