Introduction
Synopsis
Free Functions
Example
One criticism of Boost shared_array is
the lack of utility similar to make_shared
which ensures only a single allocation for an array. A second criticism
is Boost shared_array
does not support custom allocators
and so also lacks an allocate_shared
utility.
The header files <boost/smart_ptr/make_shared_array.hpp> and
<boost/smart_ptr/allocate_shared_array.hpp> provide new function
templates, make_shared
and allocate_shared
,
to address this need. make_shared
uses the global
operator new
to allocate memory, whereas
allocate_shared
uses an user-supplied allocator,
allowing finer control.
namespace boost { template<typename T> shared_ptr<T> make_shared(size_t size); template<typename T, typename A> shared_ptr<T> allocate_shared(const A& allocator, size_t size); #if defined(BOOST_HAS_VARIADIC_TMPL) && defined(BOOST_HAS_RVALUE_REFS) template<typename T, typename... Args> shared_ptr<T> make_shared(size_t size, Args&&... args); template<typename T, typename A, typename... Args> shared_ptr<T> allocate_shared(const A& allocator, size_t size, Args&&... args); #endif template<typename T> shared_ptr<T> make_shared_noinit(size_t size); }
template<typename T, typename... Args> shared_ptr<T> make_shared(size_t size, Args&&... args); template<typename T, typename A, typename... Args> shared_ptr<T> allocate_shared(const A& allocator, size_t size, Args&&... args);
Requires: The expression
new(pointer) T(std::forward<Args>(args)...)
, wherepointer
is avoid*
pointing to storage suitable to hold an object of typeT
, shall be well-formed.A
shall be an Allocator, as described in section 20.1.5 (Allocator requirements) of the C++ Standard. The copy constructor and destructor ofA
shall not throw.Effects: Allocates memory suitable for an array of type
T
and sizesize
and constructs an array of objects in it via the placement new expressionnew(pointer) T()
ornew(pointer) T(std::forward<Args>(args)...)
.allocate_shared
uses a copy ofallocator
to allocate memory. If an exception is thrown, has no effect.Returns: A
shared_ptr
instance that stores and owns the address of the newly constructed array of typeT
and sizesize
.Postconditions:
get() != 0 && use_count() == 1
.Throws:
bad_alloc
, or an exception thrown fromA::allocate
or the constructor ofT
.Notes: This implementation allocates the memory required for the returned
shared_ptr
and an array of typeT
of sizesize
in a single allocation. This provides efficiency to equivalent to an intrusive smart array pointer.The prototypes shown above are used if your compiler supports r-value references and variadic templates. They perfectly forward the
args
parameters to the constructors ofT
for each array element.Otherwise, you can use the overloads which take only the array size (and the allocator in case of
allocate_shared
) and do not take any constructor arguments. These overloads invoke the default constructor ofT
for each array element.
boost::shared_ptr<int[]> array = boost::make_shared<int[]>(size);
$Date: 2012-10-30 10:12:25 -0800 (Tue, 30 Oct 2012) $
Copyright 2012 Glen Fernandes. Distributed under the Boost Software License, Version 1.0. See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.