Originally the Boost function templates allocate_shared
and
make_shared
were for efficient allocation of shared scalar
objects only. There was a need to have efficient allocation of shared
arrays. One criticism of class template shared_array
was always the lack of a utility like make_shared
that
uses only a single allocation.
The header files <boost/smart_ptr/allocate_shared_array.hpp> and
<boost/smart_ptr/make_shared_array.hpp> provide function
templates, overloads of allocate_shared
and
make_shared
for array types, to address this need.
allocate_shared
uses a user-supplied allocator for
allocation, while make_shared
uses
allocate_shared
with the Default Allocator.
namespace boost {
template<class T, class A>
shared_ptr<T>
allocate_shared(const A& a,
std::size_t n);
template<class T, class A>
shared_ptr<T>
allocate_shared(const A& a);
template<class T, class A>
shared_ptr<T>
allocate_shared(const A& a, std::size_t n,
const E& v);
template<class T, class A>
shared_ptr<T>
allocate_shared(const A& a,
const E& v);
template<class T, class A>
shared_ptr<T>
allocate_shared_noinit(const A& a,
std::size_t n);
template<class T, class A>
shared_ptr<T>
allocate_shared_noinit(const A& a);
}
namespace boost {
template<class T, class A>
shared_ptr<T>
make_shared(std::size_t n);
template<class T, class A>
shared_ptr<T>
make_shared();
template<class T, class A>
shared_ptr<T>
make_shared(std::size_t n,
const E& v);
template<class T, class A>
shared_ptr<T>
make_shared(const E& v);
template<class T, class A>
shared_ptr<T>
make_shared_noinit(std::size_t n);
template<class T, class A>
shared_ptr<T>
make_shared_noinit();
}
template<class T, class A>
shared_ptr<T>
allocate_shared(const A& a, args);
T
is of the form E[N]
or
E[]
. A
shall be an Allocator, as
described in section 17.6.3.5 [Allocator requirements] of the C++
Standard. The copy constructor and destructor of A
shall
not throw exceptions.E
(or
E[size]
when T
is E[]
, where
size
is determined from args
as specified by
the concrete overload). A copy of the allocator is used to allocate
storage. The storage is initialized as specified by the concrete
overload. If an exception is thrown, the functions have no effect.shared_ptr
instance that stores and owns the address
of the newly allocated and constructed object.r.get() != 0
and r.use_count() == 1
,
where r
is the return value.A::allocate()
, or from the
initialization of the object.T
is specified to be
initialized to a value of the same type v
, this shall be
interpreted to mean that each array element of the object is initialized
to the corresponding element from v
.T
is specified to be
value-initialized, this shall be interpreted to mean that each array
element of the object is value-initialized.S
is specified to
be initialized to a value v
, allocate_shared
shall perform this initialization via the expression
std::allocator_traits<A>::construct(b, p, v)
, where
p
points to storage suitable to hold an object of type
S
and b
of is a copy of the allocator
a
passed to allocate_shared
such that its
value_type
is S
.S
is specified to be
value-initialized, allocate_shared
shall perform this
initialization via the expression
std::allocator_traits<A>::construct(b, p)
, where
p
points to storage suitable to hold an object
of type S
and b
is a copy of the allocator
a
passed to allocate_shared
such that its
value_type
is S
.S
is specified to be
default-initialized, allocate_shared_noinit
shall perform
this initialization via the expression ::new(p) S
, where
p
has type void*
and points to storage
suitable to hold an object of type S
.sizeof(E)
to allow for internal bookkeeping structures such
as the reference counts.template<class T, class A>
shared_ptr<T>
allocate_shared(const A& a, std::size_t n);
shared_ptr
to a value-initialized object of type
E[size]
.T
is of the form E[]
.boost::allocate_shared<int[]>(std::allocator<int>(), 8);
template<class T, class A>
shared_ptr<T>
allocate_shared(const A& a);
shared_ptr
to a value-initialized object of type
E[N]
.T
is of the form E[N]
.boost::allocate_shared<int[8]>(std::allocator<int>());
template<class T, class A>
shared_ptr<T>
allocate_shared(const A& a, std::size_t n,
const E& v);
shared_ptr
to an object of type
E[size]
, where each array element of type E
is
initialized to v
.T
is of the form E[]
.boost::allocate_shared<double[]>(std::allocator<double>(), 8, 1.0);
template<class T, class A>
shared_ptr<T>
allocate_shared(const A& a, const E& v);
shared_ptr
to an object of type E[N]
,
where each array element of type E
is initialized to
v
.T
is of the form E[N]
.boost::allocate_shared<double[8]>(std::allocator<double>(), 1.0);
template<class T, class A>
shared_ptr<T>
allocate_shared_noinit(const A& a, std::size_t n);
shared_ptr
to a default-initialized object of type
E[size]
.T
is of the form E[]
.boost::allocate_shared_noinit<int[]>(std::allocator<int>(), 8);
template<class T, class A>
shared_ptr<T>
allocate_shared_noinit(const A& a);
shared_ptr
to a default-initialized object of type
E[N]
.T
is of the form E[N]
.boost::allocate_shared_noinit<int[8]>(std::allocator<int>());
template<class T>
shared_ptr<T>
make_shared(std::size_t n);
allocate_shared<T>(std::allocator<S>(), n);
T
is of the form E[]
.boost::make_shared<int[]>(8);
template<class T>
shared_ptr<T>
make_shared();
allocate_shared<T>(std::allocator<S>());
T
is of the form E[N]
.boost::make_shared<int[8]>();
template<class T>
shared_ptr<T>
make_shared(std::size_t n, const E& v);
allocate_shared<T>(std::allocator<S>(), n, v);
T
is of the form E[]
.boost::make_shared<double[]>(8, 1.0);
template<class T>
shared_ptr<T>
make_shared(const E& v);
allocate_shared<T>(std::allocator<S>(), v);
T
is of the form E[N].
boost::make_shared<double[8]>(1.0);
template<class T>
shared_ptr<T>
make_shared_noinit(std::size_t n);
allocate_shared_noinit<T>(std::allocator<S>(), n);
T
is of the form E[]
.boost::make_shared_noinit<int[]>(8);
template<class T>
shared_ptr<T>
make_shared_noinit();
allocate_shared_noinit<T>(std::allocator<S>());
T
is of the form E[N]
.boost::make_shared_noinit<int[8]>();