Improve documentation for make_shared for arrays

This commit is contained in:
Glen Fernandes
2014-02-03 16:42:57 -08:00
parent 63a05a3576
commit 2b033ce05d
2 changed files with 272 additions and 160 deletions
+96 -72
View File
@@ -7,16 +7,16 @@
<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>
<p><a href="#introduction">Introduction</a><br>
<a href="#synopsis">Synopsis</a><br>
<a href="#common">Common Requirements</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>
<h2><a name="introduction">Introduction</a></h2>
<p>The header file &lt;boost/make_unique.hpp&gt; 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>
<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>();
@@ -29,103 +29,127 @@
template&lt;typename U&gt; // U is not array
unique_ptr&lt;U&gt; <a href="#functions">make_unique</a>(U&amp;&amp; value);
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 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>
<h2><a name="common">Common Requirements</a></h2>
<pre>template&lt;typename U&gt;
unique_ptr&lt;U&gt; make_unique(<em>args</em>);
template&lt;typename U&gt;
unique_ptr&lt;U&gt; make_unique_noinit(<em>args</em>);</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>Effects:</b> Allocates memory for an object of type <code>U</code>
(or <code>T[size]</code> when <code>U</code> is <code>T[]</code>,
where <code>size</code> is determined from <code>args</code> as
specified by the concrete overload). The object is initialized from
<code>args</code> as specified by the concrete overload. If an
exception is thrown, the functions have no effect.</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>Postconditions:</b> <code>r.get() != 0</code>, where
<code>r</code> is the return value.</p>
<p><b>Throws:</b> <code>bad_alloc</code>, or an exception thrown from
the constructor of <code>U</code>.</p>
the initialization of the object.</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>
<p><b>Remarks:</b></p>
<blockquote>
<p>When an object of a non-array type <code>T</code> is specified to
be initialized to a value <code>value</code>, or to
<code>T(list...)</code>, where <code>list...</code> is a list of
constructor arguments, <code>make_unique</code> shall perform this
initialization via the expression <code>new T(value)</code> or
<code>new T(list...)</code> respectively.</p>
<p>When an object of type <code>T</code> is specified to be
value-initialized, <code>make_unique</code> shall perform this
initialization via the expression <code>new T()</code>.</p>
<p>When an object of type <code>T</code> is specified to be
default-initialized, <code>make_unique_noinit</code> shall perform
this initialization via the expression <code>new T</code>.</p>
</blockquote>
</blockquote>
<pre>template&lt;typename U&gt; // U is not array
<h2><a name="functions">Free Functions</a></h2>
<pre>template&lt;typename U, typename... Args&gt;
unique_ptr&lt;U&gt; make_unique(Args&amp;&amp;... args);</pre>
<blockquote>
<p><b>Returns:</b> A unique_ptr to an object of type <code>U</code>,
initialized to <code>U(forward&lt;Args&gt;(args)...)</code>.</p>
<p><b>Remarks:</b> This overload shall only participate in overload
resolution when <code>U</code> is not an array type.</p>
</blockquote>
<blockquote>
<p><b>Examples:</b></p>
<blockquote>
<pre>unique_ptr&lt;float&gt; p1 = boost::make_unique&lt;float&gt;();
unique_ptr&lt;point&gt; p2 = boost::make_unique&lt;point&gt;(x, y);</pre>
</blockquote>
</blockquote>
<pre>template&lt;typename U&gt;
unique_ptr&lt;U&gt; make_unique(U&amp;&amp; value);</pre>
<blockquote>
<p><b>Requires:</b> The expression <code>new U(move(value))</code> shall
be well-formed.</p>
<p><b>Effects:</b> Constructs an object of type <code>U</code> via the
expression <code>new U(move(value))</code>.</p>
<p><b>Returns:</b> A unique_ptr to an object of type <code>U</code>,
initialized to <code>move(value)</code>.</p>
<p><b>Remarks:</b> This overload shall only participate in overload
resolution when <code>U</code> is not an array type.</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>
<p><b>Examples:</b></p>
<blockquote>
<pre>unique_ptr&lt;string&gt; p1 = boost::make_unique&lt;string&gt;({'a', 'b'});
unique_ptr&lt;point&gt; p2 = boost::make_unique&lt;point&gt;({-10, 25});</pre>
</blockquote>
</blockquote>
<pre>template&lt;typename U&gt; // U is T[]
unique_ptr&lt;U&gt; make_unique(size_t size);</pre>
<pre>template&lt;typename U&gt;
unique_ptr&lt;U&gt; 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>
<p><b>Returns:</b> A unique_ptr to a value-initialized object of type
<code>T[size]</code>.</p>
<p><b>Remarks:</b> This overload shall only participate in overload
resolution when <code>U</code> is of the form <code>T[]</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 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>unique_ptr&lt;float&gt; p1 = boost::make_unique&lt;float&gt;();
unique_ptr&lt;point&gt; p2 = boost::make_unique&lt;point&gt;();</pre>
<p><b>Examples:</b></p>
<blockquote>
<pre>unique_ptr&lt;double[]&gt; p1 = boost::make_unique&lt;double[]&gt;(4);
unique_ptr&lt;int[][2]&gt; p2 = boost::make_unique&lt;int[][2]&gt;(2);</pre>
</blockquote>
</blockquote>
<p>For objects with construction arguments.</p>
<pre>template&lt;typename U&gt;
unique_ptr&lt;U&gt; make_unique_noinit();</pre>
<blockquote>
<pre>unique_ptr&lt;float&gt; p3 = boost::make_unique&lt;float&gt;(1.0f);
unique_ptr&lt;point&gt; p4 = boost::make_unique&lt;point&gt;(x, y);</pre>
<p><b>Returns:</b> A unique_ptr to a default-initialized object of
type <code>U</code>.</p>
<p><b>Remarks:</b> This overload shall only participate in overload
resolution when <code>U</code> is not an array type.</p>
</blockquote>
<p>For objects with given value.</p>
<blockquote>
<pre>unique_ptr&lt;string&gt; p4 = boost::make_unique&lt;string&gt;({'a', 'b'});
unique_ptr&lt;type&gt; p5 = boost::make_unique&lt;type&gt;({3, 5, 4});</pre>
<p><b>Examples:</b></p>
<blockquote>
<pre>unique_ptr&lt;float&gt; p1 = boost::make_unique_noinit&lt;float&gt;();
unique_ptr&lt;point&gt; p2 = boost::make_unique_noinit&lt;point&gt;();</pre>
</blockquote>
</blockquote>
<p>For objects with default-initialization.</p>
<pre>template&lt;typename U&gt;
unique_ptr&lt;U&gt; make_unique_noinit(size_t size);</pre>
<blockquote>
<pre>unique_ptr&lt;float&gt; p6 = boost::make_unique_noinit&lt;float&gt;();
unique_ptr&lt;point&gt; p7 = boost::make_unique_noinit&lt;point&gt;();</pre>
<p><b>Returns:</b> A unique_ptr to a default-initialized object of
type <code>T[size]</code>.</p>
<p><b>Remarks:</b> This overload shall only participate in overload
resolution when <code>U</code> is of the form <code>T[]</code>.</p>
</blockquote>
<p>For arrays with value-initialization.</p>
<blockquote>
<pre>unique_ptr&lt;double[]&gt; a1 = boost::make_unique&lt;double[]&gt;();
unique_ptr&lt;int[][4]&gt; a2 = boost::make_unique&lt;int[][4]&gt;();</pre>
<p><b>Examples:</b></p>
<blockquote>
<pre>unique_ptr&lt;double[]&gt; p1 = boost::make_unique_noinit&lt;double[]&gt;(4);
unique_ptr&lt;int[][2]&gt; p2 = boost::make_unique_noinit&lt;int[][2]&gt;(2);</pre>
</blockquote>
</blockquote>
<p>For arrays with default-initialization.</p>
<blockquote>
<pre>unique_ptr&lt;double[]&gt; a3 = boost::make_unique_noinit&lt;double[]&gt;();
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>