Add top-level make_unique.hpp and documentation

This commit is contained in:
Glen Fernandes
2014-01-29 07:25:30 -08:00
parent ad658fa5ec
commit 7fef3bb40b
3 changed files with 146 additions and 0 deletions

View 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
View 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 &lt;boost/make_unique.hpp&gt; 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&lt;typename U&gt; // U is not array
unique_ptr&lt;U&gt; <a href="#functions">make_unique</a>();
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template&lt;typename U, typename... Args&gt; // U is not array
unique_ptr&lt;U&gt; <a href="#functions">make_unique</a>(Args&amp;&amp;... args);
#endif
template&lt;typename U&gt; // U is not array
unique_ptr&lt;U&gt; <a href="#functions">make_unique_noinit</a>();
template&lt;typename U&gt; // U is T[]
unique_ptr&lt;U&gt; <a href="#functions">make_unique</a>(size_t size);
template&lt;typename U&gt; // U is T[]
unique_ptr&lt;U&gt; <a href="#functions">make_unique_noinit</a>(size_t size);
}</pre>
<h2><a name="functions">Free Functions</a></h2>
<pre>template&lt;typename U&gt; // U is not array
unique_ptr&lt;U&gt; 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&lt;typename U, typename... Args&gt; // U is not array
unique_ptr&lt;U&gt; make_unique(Args&amp;&amp;... args);</pre>
<blockquote>
<p><b>Requires:</b> The expression
<code>new U(forward&lt;Args&gt;(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&lt;Args&gt;(args)...)</code>.</p>
</blockquote>
<pre>template&lt;typename U&gt; // U is not array
unique_ptr&lt;U&gt; 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&lt;typename U&gt; // U is T[]
unique_ptr&lt;U&gt; 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&lt;typename U&gt; // U is T[]
unique_ptr&lt;U&gt; 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&lt;float&gt; p1 = boost::make_unique&lt;float&gt;();
std::unique_ptr&lt;point&gt; p2 = boost::make_unique&lt;point&gt;();</pre>
</blockquote>
<p>For objects with construction arguments.</p>
<blockquote>
<pre>std::unique_ptr&lt;float&gt; p3 = boost::make_unique&lt;float&gt;(1.5);
std::unique_ptr&lt;point&gt; p4 = boost::make_unique&lt;point&gt;(x, y);</pre>
</blockquote>
<p>For objects with default-initialization.</p>
<blockquote>
<pre>std::unique_ptr&lt;float&gt; p4 = boost::make_unique_noinit&lt;float&gt;();
std::unique_ptr&lt;point&gt; p5 = boost::make_unique_noinit&lt;point&gt;();</pre>
</blockquote>
<p>For arrays with value-initialization.</p>
<blockquote>
<pre>std::unique_ptr&lt;double[]&gt; a1 = boost::make_unique&lt;double[]&gt;();
std::unique_ptr&lt;int[][4]&gt; a2 = boost::make_unique&lt;int[][4]&gt;();</pre>
</blockquote>
<p>For arrays with default-initialization.</p>
<blockquote>
<pre>std::unique_ptr&lt;double[]&gt; a3 = boost::make_unique_noinit&lt;double[]&gt;();
std::unique_ptr&lt;int[][4]&gt; a4 = boost::make_unique_noinit&lt;int[][4]&gt;();</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>

View File

@ -75,6 +75,11 @@
<td><a href="../../boost/make_shared.hpp">&lt;boost/make_shared.hpp&gt;</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">&lt;boost/make_unique.hpp&gt;</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