2012-11-06 14:17:32 +00:00
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<title>make_shared and allocate_shared</title>
|
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
|
|
|
</head>
|
|
|
|
|
<body text="#000000" bgColor="#ffffff" link="#0000ff" vlink="#0000ff">
|
2014-01-23 21:16:51 -08:00
|
|
|
<h1><img height="86" alt="boost.png (6897 bytes)" src="../../boost.png"
|
2012-11-06 14:17:32 +00:00
|
|
|
width="277" align="middle" border="0">make_shared and allocate_shared
|
|
|
|
|
for arrays</h1>
|
|
|
|
|
<p><A href="#Introduction">Introduction</A><br>
|
|
|
|
|
<A href="#Synopsis">Synopsis</A><br>
|
|
|
|
|
<A href="#functions">Free Functions</A><br>
|
|
|
|
|
<A href="#example">Example</A><br>
|
2012-12-01 22:43:57 +00:00
|
|
|
<A href="#history">History</A><br></p>
|
2012-11-06 14:17:32 +00:00
|
|
|
<h2><a name="Introduction">Introduction</a></h2>
|
2012-11-20 03:00:02 +00:00
|
|
|
<p>Originally the Boost function templates <code>make_shared</code> and
|
|
|
|
|
<code>allocate_shared</code> were for efficient allocation of single
|
|
|
|
|
objects only. There was a need to have efficient, single, allocation of
|
|
|
|
|
arrays. One criticism of <a href="shared_array.htm">shared_array</a> was
|
2013-01-20 00:09:27 +00:00
|
|
|
always the lack of a <a href="make_shared.html">make_shared</a> utility
|
2012-11-20 03:00:02 +00:00
|
|
|
which ensures only a single allocation for an array.</p>
|
2012-11-06 14:17:32 +00:00
|
|
|
<p>The header files <boost/smart_ptr/make_shared_array.hpp> and
|
|
|
|
|
<boost/smart_ptr/allocate_shared_array.hpp> provide new function
|
|
|
|
|
templates, <code>make_shared</code> and <code>allocate_shared</code>,
|
|
|
|
|
to address this need. <code>make_shared</code> uses the global
|
|
|
|
|
operator <code>new</code> to allocate memory, whereas
|
|
|
|
|
<code>allocate_shared</code> uses an user-supplied allocator,
|
|
|
|
|
allowing finer control.</p>
|
|
|
|
|
<h2><a name="Synopsis">Synopsis</a></h2>
|
|
|
|
|
<pre>namespace boost {
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U> // U is T[]
|
2013-04-10 05:00:52 +00:00
|
|
|
shared_ptr<U> <a href="#functions">make_shared</a>(size_t size);
|
2012-11-06 14:17:32 +00:00
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U, typename A> // U is T[]
|
2013-04-10 05:00:52 +00:00
|
|
|
shared_ptr<U> <a href="#functions">allocate_shared</a>(const A& allocator, size_t size);
|
2014-01-23 21:16:51 -08:00
|
|
|
|
|
|
|
|
template<typename U> // U is T[N]
|
|
|
|
|
shared_ptr<U> <a href="#functions">make_shared</a>();
|
2012-11-20 04:07:58 +00:00
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U, typename A> // U is T[N]
|
|
|
|
|
shared_ptr<U> <a href="#functions">allocate_shared</a>(const A& allocator);
|
|
|
|
|
|
|
|
|
|
template<typename U> // U is T[]
|
|
|
|
|
shared_ptr<U> <a href="#functions">make_shared</a>(size_t size, const T& value);
|
2012-11-20 04:07:58 +00:00
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U, typename A> // U is T[]
|
|
|
|
|
shared_ptr<U> <a href="#functions">allocate_shared</a>(const A& allocator, size_t size, const T& value);
|
2012-11-20 04:07:58 +00:00
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U> // U is T[N]
|
|
|
|
|
shared_ptr<U> <a href="#functions">make_shared</a>(const T& value);
|
2013-04-10 05:00:52 +00:00
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U, typename A> // U is T[N]
|
|
|
|
|
shared_ptr<U> <a href="#functions">allocate_shared</a>(const A& allocator, const T& value);
|
2012-12-01 05:23:37 +00:00
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U> // U is T[]
|
2013-04-10 05:00:52 +00:00
|
|
|
shared_ptr<U> <a href="#functions">make_shared_noinit</a>(size_t size);
|
|
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U, typename A> // U is T[]
|
2013-04-10 05:00:52 +00:00
|
|
|
shared_ptr<U> <a href="#functions">allocate_shared_noinit</a>(const A& allocator, size_t size);
|
2012-12-11 17:42:47 +00:00
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U> // U is T[N]
|
|
|
|
|
shared_ptr<U> <a href="#functions">make_shared_noinit</a>();
|
|
|
|
|
|
|
|
|
|
template<typename U, typename A> // U is T[N]
|
2013-04-10 05:00:52 +00:00
|
|
|
shared_ptr<U> <a href="#functions">allocate_shared_noinit</a>(const A& allocator);
|
2012-11-06 14:17:32 +00:00
|
|
|
}</pre>
|
|
|
|
|
<h2><a name="functions">Free Functions</a></h2>
|
2014-01-23 21:16:51 -08:00
|
|
|
<pre>template<typename U> // U is T[]
|
|
|
|
|
shared_ptr<U> make_shared(size_t size);
|
2013-04-10 05:16:09 +00:00
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U, typename A> // U is T[]
|
|
|
|
|
shared_ptr<U> allocate_shared(const A& allocator, size_t size);
|
|
|
|
|
|
|
|
|
|
template<typename U> // U is T[N]
|
|
|
|
|
shared_ptr<U> make_shared();
|
|
|
|
|
|
|
|
|
|
template<typename U, typename A> // U is T[N]
|
|
|
|
|
shared_ptr<U> allocate_shared(const A& allocator);</pre>
|
2012-11-06 14:17:32 +00:00
|
|
|
<blockquote>
|
|
|
|
|
<p><b>Requires:</b> The expression
|
2014-01-23 21:16:51 -08:00
|
|
|
<code>new(pointer) T()</code>, where <code>pointer</code> is a
|
|
|
|
|
<code>void*</code> pointing to storage suitable to hold an object
|
|
|
|
|
of type <code>T</code>, shall be well-formed. <code>A</code>
|
|
|
|
|
shall be an <em>Allocator</em>, as described in section 20.1.5
|
|
|
|
|
(<strong>Allocator requirements</strong>) of the C++ Standard.
|
|
|
|
|
The copy constructor and destructor of <code>A</code> shall not
|
|
|
|
|
throw.</p>
|
2012-11-06 14:17:32 +00:00
|
|
|
<p><b>Effects:</b> Allocates memory suitable for an array of type
|
|
|
|
|
<code>T</code> and size <code>size</code> and constructs an array
|
|
|
|
|
of objects in it via the placement new expression
|
2014-01-23 21:16:51 -08:00
|
|
|
<code>new(pointer) T()</code>. <code>allocate_shared</code> uses
|
|
|
|
|
a copy of <code>allocator</code> to allocate memory. If an
|
|
|
|
|
exception is thrown, has no effect.</p>
|
2012-11-06 14:17:32 +00:00
|
|
|
<p><b>Returns:</b> A <code>shared_ptr</code> instance that stores and
|
|
|
|
|
owns the address of the newly constructed array of type <code>T</code>
|
|
|
|
|
and size <code>size</code>.</p>
|
|
|
|
|
<p><b>Postconditions:</b>
|
|
|
|
|
<code>get() != 0 && use_count() == 1</code>.</p>
|
|
|
|
|
<p><b>Throws:</b> <code>bad_alloc</code>, or an exception thrown from
|
|
|
|
|
<code>A::allocate</code> or the constructor of <code>T</code>.</p>
|
|
|
|
|
<p><b>Notes:</b> This implementation allocates the memory required for
|
|
|
|
|
the returned <code>shared_ptr</code> and an array of type
|
|
|
|
|
<code>T</code> of size <code>size</code> in a single allocation. This
|
|
|
|
|
provides efficiency to equivalent to an intrusive smart array
|
2014-01-23 21:16:51 -08:00
|
|
|
pointer.</p>
|
2012-11-06 14:17:32 +00:00
|
|
|
</blockquote>
|
2014-01-23 21:16:51 -08:00
|
|
|
<pre>template<typename U> // U is T[]
|
|
|
|
|
shared_ptr<U> make_shared(size_t size, const T& value);
|
2013-04-10 05:16:09 +00:00
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U, typename A> // U is T[]
|
|
|
|
|
shared_ptr<U> allocate_shared(const A& allocator, size_t size, const T& value);
|
2013-04-10 05:16:09 +00:00
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U> // U is T[N]
|
|
|
|
|
shared_ptr<U> make_shared(const T& value);
|
2013-04-10 05:16:09 +00:00
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U, typename A> // U is T[N]
|
|
|
|
|
shared_ptr<U> allocate_shared(const A& allocator, const T& value);</pre>
|
2012-12-04 06:06:23 +00:00
|
|
|
<blockquote>
|
|
|
|
|
<p><b>Description:</b> These overloads initialize array elements with
|
|
|
|
|
the given value.</p>
|
|
|
|
|
</blockquote>
|
2014-01-23 21:16:51 -08:00
|
|
|
<pre>template<typename U> // U is T[]
|
2013-04-10 05:16:09 +00:00
|
|
|
shared_ptr<U> make_shared_noinit(size_t size);
|
|
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U, typename A> // U is T[]
|
|
|
|
|
shared_ptr<U> allocate_shared_noinit(const A& allocator, size_t size);
|
|
|
|
|
|
|
|
|
|
template<typename U> // U is T[N]
|
2013-04-10 05:16:09 +00:00
|
|
|
shared_ptr<U> make_shared_noinit();
|
|
|
|
|
|
2014-01-23 21:16:51 -08:00
|
|
|
template<typename U, typename A> // U is T[N]
|
2013-04-10 05:16:09 +00:00
|
|
|
shared_ptr<U> allocate_shared_noinit(const A& allocator);</pre>
|
2012-11-20 03:00:02 +00:00
|
|
|
<blockquote>
|
2014-01-23 21:16:51 -08:00
|
|
|
<p><b>Description:</b> These overloads do not perform any value
|
|
|
|
|
initialization of elements.</p>
|
|
|
|
|
</blockquote>
|
|
|
|
|
<h2><a name="example">Examples</a></h2>
|
|
|
|
|
<p>Some examples of each overload of make_shared for arrays:</p>
|
2012-11-18 01:51:46 +00:00
|
|
|
<blockquote>
|
2013-04-10 05:00:52 +00:00
|
|
|
<pre>boost::shared_ptr<int[]> a1 = boost::make_shared<int[]>(size);
|
2014-01-23 21:16:51 -08:00
|
|
|
boost::shared_ptr<int[8]> a2 = boost::make_shared<int[8]>();
|
|
|
|
|
boost::shared_ptr<int[][2]> a3 = boost::make_shared<int[][2]>(size);
|
|
|
|
|
boost::shared_ptr<int[4][2]> a4 = boost::make_shared<int[4][2]>();
|
|
|
|
|
boost::shared_ptr<int[]> a5 = boost::make_shared<int[]>(size, 1);
|
|
|
|
|
boost::shared_ptr<int[8]> a6 = boost::make_shared<int[8]>(1);
|
|
|
|
|
boost::shared_ptr<int[][2]> a7 = boost::make_shared<int[][2]>(size, {1, 2});
|
|
|
|
|
boost::shared_ptr<int[4][2]> a8 = boost::make_shared<int[4][2]>({1, 2});
|
|
|
|
|
boost::shared_ptr<int[]> a9 = boost::make_shared_noinit<int[]>(size);
|
|
|
|
|
boost::shared_ptr<int[8]> a10 = boost::make_shared_noinit<int[8]>();
|
|
|
|
|
boost::shared_ptr<int[][2]> a11 = boost::make_shared_noinit<int[][2]>(size);
|
|
|
|
|
boost::shared_ptr<int[4][2]> a12 = boost::make_shared_noinit<int[4][2]>();</pre>
|
2012-11-18 01:51:46 +00:00
|
|
|
</blockquote>
|
2012-11-20 03:00:02 +00:00
|
|
|
<h2><a name="history">History</a></h2>
|
|
|
|
|
<p>November 2012. Glen Fernandes contributed implementations of
|
|
|
|
|
make_shared and allocate_shared for arrays.</p>
|
2012-11-06 14:17:32 +00:00
|
|
|
<hr>
|
2014-01-23 21:16:51 -08:00
|
|
|
<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>
|
2012-11-06 14:17:32 +00:00
|
|
|
</body>
|
|
|
|
|
</html>
|