mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-30 04:47:12 +02:00
Add top-level make_unique.hpp and documentation
This commit is contained in:
14
include/boost/make_unique.hpp
Normal file
14
include/boost/make_unique.hpp
Normal file
@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright (c) 2014 Glen Joseph Fernandes
|
||||
* glenfe at live dot com
|
||||
*
|
||||
* Distributed under the Boost Software License,
|
||||
* Version 1.0. (See accompanying file LICENSE_1_0.txt
|
||||
* or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#ifndef BOOST_MAKE_UNIQUE_HPP_INCLUDED
|
||||
#define BOOST_MAKE_UNIQUE_HPP_INCLUDED
|
||||
|
||||
#include <boost/smart_ptr/make_unique.hpp>
|
||||
|
||||
#endif
|
124
make_unique.html
Normal file
124
make_unique.html
Normal file
@ -0,0 +1,124 @@
|
||||
<!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 a family of
|
||||
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 U()</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 U[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 U</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 U[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>
|
@ -75,6 +75,11 @@
|
||||
<td><a href="../../boost/make_shared.hpp"><boost/make_shared.hpp></a></td>
|
||||
<td>Efficient creation of <code>shared_ptr</code> arrays.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="make_unique.html"><b>make_unique</b></a></td>
|
||||
<td><a href="../../boost/make_unique.hpp"><boost/make_unique.hpp></a></td>
|
||||
<td>Creation of <code>unique_ptr</code> objects and arrays.</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
<p>A test program, <a href="test/smart_ptr_test.cpp">smart_ptr_test.cpp</a>, is
|
||||
@ -131,6 +136,9 @@
|
||||
<p>Functions which destroy objects of the pointed to type are prohibited from
|
||||
throwing exceptions by the <a href="#common_requirements">common requirements</a>.</p>
|
||||
<h2><a name="History">History</a> and Acknowledgements</h2>
|
||||
<p>January 2014. Glen Fernandes confined the overloads of <b>make_shared</b>
|
||||
and <b>allocate_shared</b> for arrays to the specification in N3870 and
|
||||
implemented <b>make_unique</b> for arrays and objects.</p>
|
||||
<p>November 2012. Glen Fernandes provided implementations of <b>make_shared</b>
|
||||
and <b>allocate_shared</b> for arrays. They achieve a single allocation for an
|
||||
array that can be initialized with constructor arguments or initializer lists
|
||||
|
Reference in New Issue
Block a user