Boost logo

Boost :

From: Glen Fernandes (glen.fernandes_at_[hidden])
Date: 2020-01-23 13:06:52


On Wed, Jan 22, 2020 at 11:04 PM Christian Mazakas via Boost
<boost_at_[hidden]> wrote:
> `boost::allocate_unique` is a significantly tougher class to beat because
> it boasts many of the same features: a dynamic allocation done using the
> supplied Allocator supporting `noinit` construction and

Correct so far.

> array objects with a size (stored in the Deleter of the returned `std::unique_ptr`).

Wrong. The D in the unique_ptr<T, D> returned by allocate_unique()
only stores the Allocator.
The size for allocate_unique<T[]>() is stored in D::pointer. i.e.
D::pointer is a pointer-like type (detail::alloc_ptr) that stores
A::pointer and size.

But for T[N] there's no need to store the size. D::pointer is a type
that just stores A::pointer.
Same for non-array T, there's no size stored. D::pointer is a type
that just stores Only A::pointer

Which means for arrays of sizes known at compile-time,:
* dynamic_array<T, A> x(a, N); will store that extra size
* auto x = allocate_unique<T[N]>(a); will not

In addition to that, boost::allocate_unique() gives you almost
everything else dynamic_array gives you:
* Allocator aware
* Supports construction from a value
* Supports default-initialization (noinit)
* Supports arrays of arrays

> And while it is true that `boost::allocate_unique` enables all of this, it still
> lacks a few key semantics. Namely, `dynamic_array` is copyable. It's also
> copy assignable and move assignable,

Yes, this is the only difference. The question is if anyone needs
this. i.e. When they reach for something lighter than vector<T, A> do
they also want that type to be deep copyable/movable.

If they do, such an adaptor over unique_ptr<T, D> could be useful sense.

But if they just want to 'copy or move elements from the result of
allocate_unique() that's also not difficult.

Glen


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk