Programs can become more efficient
when they can bind aspects of their execution
earlier in program development.
As an example,
the std::unordered_map container
provides more functionality than std::vector,
but std::vector
provides better performance
when the programmer can bind indexes
to a dense, but extensible, range near zero.
Going further, built-in arrays
provide even better performance
by binding the range end at compilation time.

Unfortunately, for some applications,
the range end is known at container construction
but not at compilation time.
So, built-in arrays are not applicable.
On the other hand,
std::vector is more general than needed,
as it permits an extensibility that is not required.
Ideally, we would like to be able to specify a container
where the index end is bound at construction,
but does not change thereafter.

The C programming language has such a container
in the form of variable-length arrays.
They are not general in that
they are limited to automatic variables,
but given that restriction
they are nearly as efficient as normal arrays,
requiring only mark/release stack allocation
and maintenance of a frame pointer.
(Maintaining a frame pointer is a good idea anyway.)
Unfortunately the detailed type semantics of C variable-length arrays
are probably not acceptable to C++,
so we cannot simply adopt them.

The std::valarray container is intermediate
between built-in arrays and std::vector,
but as it supports a resize method,
it cannot hold its size fixed for the lifetime of the variable.
Furthermore, std::valarray
supports compound member assignment operators
that imply such operators in the parameter type.
Such implications are workable only for types with "full interfaces",
not for general types.

Instead of adopting C variable-length arrays,
we propose to define a new facility for arrays
where the number of elements is bound at construction.
We call these dynamic arrays, dynarray.
In keeping with C++ practice,
we wish to make dynarrays
usable with more than just automatic variables.
But to take advantage of the efficiency stack allocation,
we wish to make dynarray optimizable
when used as an automatic variable.

Therefore, we propose to define dynarray
so that compilers can recognize and implement
construction and destruction directly,
without appeal to any particular standard library implementation.
However, to minimize the necessary burden on compilers,
we propose that dynarray can be implemented as a pure library,
although with lost optimization opportunity.

We believe that the compilers can introduce the optimization
without impact on source or binary compatiblity.
There may be some change in code profiles and operator new calls
as a result of that optimization,
but such risks are common to compiler and library upgrades.

Syntactically, our proposal follows the lead of
std::array and std::vector containers.
Semantically, our proposal follows the lead of built-in arrays.
That is,
we do not require more out of std::dynarray element types
than we do of standard array element types.

The dynarray constructor has a parameter
indicating the number of elements in the container.
Dynarray requires an element type with a default constructor,
just as the built-in array requires.
Note that dynarray does not provide a default constructor,
because there is no reasonable default size,
and hence the dynarray
may not take a dynarray as an element.

Dynarray provides a copy constructor,
but use of the copy constructor requires that the element type
also have a copy constructor.
The presence of this constructor implies that
users cannot explicitly instantiate the dynarray template class
on a type that does not have a copy constructor.
This practice already exists in the standard library.

Dynarray also provides reverse iterators,
but these definitions imply that
the compiler implementation depends on the standard library implementation,
which is the reverse of the normal dependence.

Dynarray does not provide
any mechanism for determining whether heap or stack allocation was used.

Dynarray does not provide
a constructor from first and last forward iterators.
Such a constructor is possible, though,
as one can determine the size with std::distance(first,last).
The technical consideration is that
determining the distance is only constant time for random access iterators.

Both proposals permit allocation on the stack or on the heap,
at the discression of the implementation.

Arrays of runtime bound
can be used only for variables of automatic storage duration.
In contrast, dynarray can be used anywhere.
However, when dynarray is not used in a automatic variable,
use of the heap is necessarily required.

The types of arrays of runtime bound
are not inspectable (e.g. with decltype).
In contrast, dynarray has a normal template class type,
but the bound is not part of that type.

Arrays of runtime bound
throw std::bad_array_length
when the array size is unacceptable.
As currently defined,
dynarray also throws throw std::bad_array_length,
under the assumption that
migration between the two is easier if they throw the same exception.
The other choice would be to throw std::length_error
as does std::vector::reserve().
Note, however, that the std::vector::vector(size_type n)
does not specify an exception.
The operator new[]
throws std::bad_array_new_length.

Because it may allocate memory,
dynarray may also throw std::bad_alloc.

Arrays of runtime bounds do no allow zero size;
dynarray does.

One can use initializer lists with arrays of runtime bound.
They are not currently supported with dynarray.
If dynarray were to support initializer lists,
it would derive its size from the initializer list.
This approach prevents a specification inconsistency
that is possible in arrays of runtime bound.

The dynarray container definition is as follows.
The section, paragraph, and table references
are based on those of
N3485 Working Draft, Standard for Programming Language C++,
Stefanus Du Toit, November 2012.

The header <dynarray>
defines a class template for storing sequences of objects
where the size is fixed at construction.
A dynarray supports random access iterators.
An instance of dynarray<T>
stores elements of type T.
The elements of a dynarray are stored contiguously,
meaning that if d is an dynarray<T>
then it obeys the identity
&d[n] == &d[0] + n for all 0 <= n < d.size().

Unless otherwise specified,
all dynarray operations have the same requirements and semantics
as specified in 23.2.

Effects:
Allocates storage for c elements.
May or may not invoke the global operator new.
The c elements of the dynarray are default-initialized (8.5).

Throws:std::bad_array_length
when the size requested is larger than implementable.
std::bad_alloc
when there is insufficient memory.

dynarray(size_type c, const T& v);

Requires:T shall meet the CopyConstructible requirements.

Effects:
Allocates storage for c elements.
May or may not invoke the global operator new.
The c elements of the
dynarray are direct-initialized (8.5) with argument v.

Throws:std::bad_array_length
when the size requested is larger than implementable.
std::bad_alloc
when there is insufficient memory.

dynarray(const dynarray& d);

Requires:T shall meet the CopyConstructible requirements.

Throws:std::bad_alloc
when there is insufficient memory.

Effects:
Allocates storage for d.size() elements.
The d.size() elements of the dynarray
are direct-initialized (8.5)
with the corresponding elements of d.
May or may not invoke the global operator new.

dynarray shall provide support
for the special case of construction with a size of zero.
In the case that the size is zero,
begin() == end() == unique value.
The return value of data() is unspecified.
The effect of calling front() or back()
for a zero-sized dynarray is undefined.

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.
—end note]