From 2b033ce05db9558fcb4971afc85dbc7dd6cdb42d Mon Sep 17 00:00:00 2001
From: Glen Fernandes Introduction Introduction Originally the Boost function templates Requires: The expression
- Effects: Allocates memory suitable for an array of type
- Requires: Effects: Allocates memory for an object of type Returns: A Postconditions:
- Throws: Notes: This implementation allocates the memory required for
- the returned Notes: These overloads initialize array objects with the given
+ owns the address of the newly constructed object. Postconditions: Throws: Notes: These overloads do not perform value initialization of
- array objects. The following examples value-initialize objects. Remarks: This implementation performs no more than one memory
+ allocation. This provides efficiency to equivalent to an intrusive
+ smart pointer. When an object of an array type When an object of an array type is specified to be
+ value-initialized, this shall be interpreted to mean that each
+ array element of the object is value-initialized. Array elements are initialized in ascending order of their
+ addresses. When a subobject of a non-array type When a subobject of non-array type When a subobject of non-array type When the lifetime of the object managed by the return value ends,
+ or when the initialization of an array element throws an exception,
+ the initialized elements should be destroyed in the reverse order
+ of their construction. The following examples initialize objects with a given value. Returns: A Remarks: These overloads shall only participate in overload
+ resolution when The following examples default-initialize objects. Examples: Returns: A Remarks: These overloads shall only participate in overload
+ resolution when Examples: Returns: A Remarks: These overloads shall only participate in overload
+ resolution when Examples: Returns: A Remarks: These overloads shall only participate in overload
+ resolution when Examples: Returns: A Remarks: These overloads shall only participate in overload
+ resolution when Examples: Returns: A Remarks: These overloads shall only participate in overload
+ resolution when Examples: January 2014. Glen Fernandes reduced the overloads of make_shared and
diff --git a/make_unique.html b/make_unique.html
index 2d5544d..fc46428 100644
--- a/make_unique.html
+++ b/make_unique.html
@@ -7,16 +7,16 @@
Introduction Introduction The header file <boost/make_unique.hpp> provides overloaded
function template Requires: The expression Effects: Constructs an object of type Effects: Allocates memory for an object of type Returns: A Postconditions: Postconditions: Throws:
-
make_shared and allocate_shared
for arrays
- Synopsis
+
+ Synopsis
+ Common Requirements
Free Functions
- Examples
History
ReferencesIntroduction
+ Introduction
make_shared and
allocate_shared were for efficient allocation of shared
objects only. There was a need to have efficient allocation of
@@ -28,24 +28,24 @@
make_shared uses the global operator new to
allocate memory, whereas allocate_shared uses an
user-supplied allocator, allowing finer control.Synopsis
+ Synopsis
namespace boost {
template<typename U> // U is T[]
shared_ptr<U> make_shared(size_t size);
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);
-
+
template<typename U> // U is T[]
shared_ptr<U> make_shared(size_t size, const T& value);
- template<typename U, typename A> // U is T[]
+ template<typename U, typename A> // U is T[]
shared_ptr<U> allocate_shared(const A& allocator, size_t size, const T& value);
template<typename U> // U is T[N]
@@ -66,95 +66,183 @@
template<typename U, typename A> // U is T[N]
shared_ptr<U> allocate_shared_noinit(const A& allocator);
}
- Free Functions
- template<typename U> // U is T[]
-shared_ptr<U> make_shared(size_t size);
-
-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);
+ Common Requirements
+ template<typename U>
+ shared_ptr<U> make_shared(args);
+template<typename U, typename A>
+ shared_ptr<U> allocate_shared(const A& allocator, args);
+template<typename U>
+ shared_ptr<U> make_shared_noinit(args);
+template<typename U, typename A>
+ shared_ptr<U> allocate_shared_noinit(const A& allocator, args);
-
- new(pointer) T(), where pointer is a
- void* pointing to storage suitable to hold an object of
- type T, 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 of A shall not throw.T and size size and constructs an array of
- objects in it via the placement new expression new(pointer)
- T(). allocate_shared uses a copy of
- allocator to allocate memory. If an exception is thrown,
- has no effect.U is of the form T[] or
+ T[N]. 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.U
+ (or T[size] when U is T[],
+ where size is determined from args
+ as specified by the concrete overload). The object is initialized as
+ specified by the concrete overload. The templates
+ allocate_shared and allocate_shared_noinit
+ use a copy of allocator to allocate memory. If an
+ exception is thrown, the functions have no effect.shared_ptr instance that stores and
- owns the address of the newly constructed array of type T
- and size size.get() != 0 && use_count() == 1.bad_alloc, or an exception thrown from
- A::allocate or the constructor of T.shared_ptr and an array of type
- T of size size in a single allocation. This
- provides efficiency to equivalent to an intrusive smart pointer.template<typename U> // U is T[]
-shared_ptr<U> make_shared(size_t size, const T& value);
-
-template<typename U, typename A> // U is T[]
-shared_ptr<U> allocate_shared(const A& allocator, size_t size, const T& value);
-
-template<typename U> // U is T[N]
-shared_ptr<U> make_shared(const T& value);
-
-template<typename U, typename A> // U is T[N]
-shared_ptr<U> allocate_shared(const A& allocator, const T& value);
-
-
- r.get() != 0 &&
+ r.use_count() == 1, where r is the return
value.bad_alloc, an exception thrown from
+ A::allocate, or from the initialization of the
+ object.template<typename U> // U is T[]
-shared_ptr<U> make_shared_noinit(size_t size);
-
-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]
-shared_ptr<U> make_shared_noinit();
-
-template<typename U, typename A> // U is T[N]
-shared_ptr<U> allocate_shared_noinit(const A& allocator);
-
- Examples
-
-
- boost::shared_ptr<int[]> a1 = boost::make_shared<int[]>(size);
-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]>();
+
+
T is specified to be
+ initialized to a value of the same type value, this
+ shall be interpreted to mean that each array element of the object
+ is initialized to the corresponding element from
+ value.T is specified to
+ be initialized to a value value, or to
+ T(list...), where list... is a list of
+ constructor arguments, make_shared shall perform this
+ initialization via the expression ::new(ptr) T(value)
+ or ::new(ptr) T(list...) respectively, where
+ ptr has type void* and points to storage
+ suitable to hold an object of type T.T is specified to
+ be value-initialized, make_shared shall perform this
+ initialization via the expression ::new(ptr) T(), where
+ ptr has type void* and points to storage
+ suitable to hold an object of type T.T is specified to
+ be default-initialized, make_shared_noinit and
+ allocate_shared_noinit shall perform this
+ initialization via the expression ::new(ptr) T, where
+ ptr has type void* and points to storage
+ suitable to hold an object of type T.Free Functions
+ template<typename U>
+ shared_ptr<U> make_shared(size_t size);
+template<typename U, typename A>
+ shared_ptr<U> allocate_shared(const A& allocator, size_t size);
-
- 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});
+ shared_ptr to a value-initialized
+ object of type T[size].U is of the form T[].
-
+ 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]>();
+
+
+ boost::shared_ptr<int[]> a1 = boost::make_shared<int[]>(size);
+boost::shared_ptr<int[][2]> a2 = boost::make_shared<int[][2]>(size);
+ template<typename U>
+ shared_ptr<U> make_shared();
+template<typename U, typename A>
+ shared_ptr<U> allocate_shared(const A& allocator);
+
+
+ shared_ptr to a value-initialized
+ object of type T[N].U is of the form T[N].
+
+
+
+ boost::shared_ptr<int[8]> a1 = boost::make_shared<int[8]>();
+boost::shared_ptr<int[4][2]> a2 = boost::make_shared<int[4][2]>();
+ template<typename U>
+ shared_ptr<U> make_shared(size_t size, const T& value);
+template<typename U, typename A>
+ shared_ptr<U> allocate_shared(const A& allocator, size_t size, const T& value);
+
+
+ shared_ptr to an object of type
+ T[size], where each array element of type T
+ is initialized to value.U is of the form T[].
+
+
+
+ boost::shared_ptr<int[]> a1 = boost::make_shared<int[]>(size, 1);
+boost::shared_ptr<int[][2]> a2 = boost::make_shared<int[][2]>(size, {1, 2});
+ template<typename U>
+ shared_ptr<U> make_shared(const T& value);
+template<typename U, typename A>
+ shared_ptr<U> allocate_shared(const A& allocator, const T& value);
+
+
+ shared_ptr to an object of type
+ T[N], where each array element of type T is
+ initialized to value.U is of the form T[N].
+
+
+
+ boost::shared_ptr<int[8]> a1 = boost::make_shared<int[8]>(1);
+boost::shared_ptr<int[4][2]> a2 = boost::make_shared<int[4][2]>({1, 2});
+ template<typename U>
+ shared_ptr<U> make_shared_noinit(size_t size);
+template<typename U, typename A>
+ shared_ptr<U> allocate_shared_noinit(const A& allocator, size_t size);
+
+
+ shared_ptr to a default-initialized
+ object of type T[size].U is of the form T[].
+
+
+
+ boost::shared_ptr<int[]> a1 = boost::make_shared_noinit<int[]>(size);
+boost::shared_ptr<int[][2]> a2 = boost::make_shared_noinit<int[][2]>(size);
+ template<typename U>
+ shared_ptr<U> make_shared_noinit();
+template<typename U, typename A>
+ shared_ptr<U> allocate_shared_noinit(const A& allocator);
+
+
+ shared_ptr to a default-initialized
+ object of type T[N].U is of the form T[N].
+
+
boost::shared_ptr<int[8]> a1 = boost::make_shared_noinit<int[8]>();
+boost::shared_ptr<int[4][2]> a2 = boost::make_shared_noinit<int[4][2]>();
+ History
-
make_unique
- Synopsis
+
+ Synopsis
+ Common Requirements
Free Functions
- Examples
HistoryIntroduction
+ Introduction
make_unique for convenient creation of
unique_ptr objects.Synopsis
+ Synopsis
namespace boost {
template<typename U> // U is not array
unique_ptr<U> make_unique();
@@ -29,103 +29,127 @@
template<typename U> // U is not array
unique_ptr<U> make_unique(U&& value);
+ template<typename U> // U is T[]
+ unique_ptr<U> make_unique(size_t size);
+
template<typename U> // U is not array
unique_ptr<U> make_unique_noinit();
- template<typename U> // U is T[]
- unique_ptr<U> make_unique(size_t size);
-
template<typename U> // U is T[]
unique_ptr<U> make_unique_noinit(size_t size);
}
- Free Functions
- template<typename U> // U is not array
-unique_ptr<U> make_unique();
+ Common Requirements
+ template<typename U>
+ unique_ptr<U> make_unique(args);
+template<typename U>
+ unique_ptr<U> make_unique_noinit(args);
-
new U() shall be
- well-formed.U via the
- expression new U().U
+ (or T[size] when U is T[],
+ where size is determined from args as
+ specified by the concrete overload). The object is initialized from
+ args as specified by the concrete overload. If an
+ exception is thrown, the functions have no effect.unique_ptr instance that stores and
owns the address of the newly constructed object.get() != 0.r.get() != 0, where
+ r is the return value.bad_alloc, or an exception thrown from
- the constructor of U.
template<typename U, typename... Args> // U is not array -unique_ptr<U> make_unique(Args&&... args);
--Requires: The expression -
-new U(forward<Args>(args)...)shall be - well-formed.Effects: Constructs an object of type
+Uvia the - expressionnew U(forward<Args>(args)...).Remarks:
++When an object of a non-array type
+Tis specified to + be initialized to a valuevalue, or to +T(list...), wherelist...is a list of + constructor arguments,make_uniqueshall perform this + initialization via the expressionnew T(value)or +new T(list...)respectively.When an object of type
+Tis specified to be + value-initialized,make_uniqueshall perform this + initialization via the expressionnew T().When an object of type
+Tis specified to be + default-initialized,make_unique_noinitshall perform + this initialization via the expressionnew T.
template<typename U> // U is not array +Free Functions
+template<typename U, typename... Args> +unique_ptr<U> make_unique(Args&&... args);+++Returns: A unique_ptr to an object of type
+U, + initialized toU(forward<Args>(args)...).Remarks: This overload shall only participate in overload + resolution when
+Uis not an array type.++Examples:
+++unique_ptr<float> p1 = boost::make_unique<float>(); +unique_ptr<point> p2 = boost::make_unique<point>(x, y);+template<typename U> unique_ptr<U> make_unique(U&& value);--Requires: The expression
-new U(move(value))shall - be well-formed.Effects: Constructs an object of type
+Uvia the - expressionnew U(move(value)).Returns: A unique_ptr to an object of type
+U, + initialized tomove(value).Remarks: This overload shall only participate in overload + resolution when
Uis not an array type.template<typename U> // U is not array -unique_ptr<U> make_unique_noinit();--Requires: The expression
-new Ushall be - well-formed.Effects: Constructs an object of type
+Uvia the - expressionnew U.Examples:
++unique_ptr<string> p1 = boost::make_unique<string>({'a', 'b'}); +unique_ptr<point> p2 = boost::make_unique<point>({-10, 25});+template<typename U> // U is T[] -unique_ptr<U> make_unique(size_t size);+template<typename U> +unique_ptr<U> make_unique(size_t size);--Requires: The expression
-new T[size]()shall be - well-formed.Effects: Constructs an array of objects of type -
+Uand sizesizevia the expression -new T[size]().Returns: A unique_ptr to a value-initialized object of type +
+T[size].Remarks: This overload shall only participate in overload + resolution when
Uis of the formT[].template<typename U> // U is T[] -unique_ptr<U> make_unique_noinit(size_t size);--Requires: The expression
-new T[size]shall be - well-formed.Effects: Constructs an array of objects of type -
-Uand sizesizevia the expression -new T[size].Examples
-For objects with value-initialization.
---unique_ptr<float> p1 = boost::make_unique<float>(); -unique_ptr<point> p2 = boost::make_unique<point>();+Examples:
++unique_ptr<double[]> p1 = boost::make_unique<double[]>(4); +unique_ptr<int[][2]> p2 = boost::make_unique<int[][2]>(2);+For objects with construction arguments.
+template<typename U> +unique_ptr<U> make_unique_noinit();--unique_ptr<float> p3 = boost::make_unique<float>(1.0f); -unique_ptr<point> p4 = boost::make_unique<point>(x, y);+Returns: A unique_ptr to a default-initialized object of + type
+U.Remarks: This overload shall only participate in overload + resolution when
Uis not an array type.For objects with given value.
--unique_ptr<string> p4 = boost::make_unique<string>({'a', 'b'}); -unique_ptr<type> p5 = boost::make_unique<type>({3, 5, 4});+Examples:
++unique_ptr<float> p1 = boost::make_unique_noinit<float>(); +unique_ptr<point> p2 = boost::make_unique_noinit<point>();+For objects with default-initialization.
+template<typename U> +unique_ptr<U> make_unique_noinit(size_t size);--unique_ptr<float> p6 = boost::make_unique_noinit<float>(); -unique_ptr<point> p7 = boost::make_unique_noinit<point>();+Returns: A unique_ptr to a default-initialized object of + type
+T[size].Remarks: This overload shall only participate in overload + resolution when
Uis of the formT[].For arrays with value-initialization.
--unique_ptr<double[]> a1 = boost::make_unique<double[]>(); -unique_ptr<int[][4]> a2 = boost::make_unique<int[][4]>();+Examples:
++unique_ptr<double[]> p1 = boost::make_unique_noinit<double[]>(4); +unique_ptr<int[][2]> p2 = boost::make_unique_noinit<int[][2]>(2);+For arrays with default-initialization.
--unique_ptr<double[]> a3 = boost::make_unique_noinit<double[]>(); -unique_ptr<int[][4]> a4 = boost::make_unique_noinit<int[][4]>();-History
January 2014. Glen Fernandes contributed implementations of make_unique for objects and arrays.