As you said yourself, std::dynarray
is for a fixed-size dynamic array. It is not resizable. It's roughly speaking an improvement over new T[N]
and over std::unique_ptr<T[]>(new T[N])
.
Not needing to resize or manage capacity means you can implement the data structure with less complexity and in less space.
Moreover, std::dynarray
is a weird animal that allows the implementation to implement it in different, non-specific ways, e.g. it's possible to put the array on the stack. Calling an allocation function is "optional". You can specify an allocator to construct the elements of the array, but that is not part of the type.
You might also wonder why we need std::dynarray
and variable-length arrays. VLAs in C++14 are much more restrictive; they can only be local, automatic variables and offer no way to specify an allocation policy, and of course they don't have a standard container interface.
Some examples from 23.3.4.2 of a "current draft" (take that, Google cache):
explicit dynarray(size_type c);
Effects: Allocates storage for c
elements. May or may not invoke the global operator new
.
template <class Alloc>
dynarray(size_type c, const Alloc& alloc);
Effects: Equivalent to the preceding constructors except that each element is constructed with uses-allocator construction.
Whether or not you can use a given allocator to construct the array elements is a global trait:
template
struct uses_allocator, Alloc> : true_type { };
Requires: Alloc
shall be an Allocator (17.6.3.5). [Note: Specialization of this trait informs other library components that dynarray
can be constructed with an allocator, even though it does not have a nested allocator_type.]
Edit: Jonathan Wakely's answer is bound to be far more authoritative and insightful.