Have you considered using an underlying array containing elements of union type for the static_vector implementation?
Fwiiw I implemented my own StaticVector< T, N, A > with the following goals:
usable in constexpr expressions (unlike EASTL's eastl::fixed_vector);
not relying on operator new/dynamic memory allocation to support compile-time initialization and runtime usage (unlike C++20's std::vector);
not requiring value types to be default constructible and/or trivially destructible (unlike wrapping a std::array/C-style array).
I was inspired by std::optional< T > to achieve the latter.
One cannot use an array of std::optional< T > elements directly, because besides wrapping a union, std::optional< T > needs to keep track of the active union member as well. This bookkeeping is redundant for each array element. Furthermore, std::optional< T > has a larger size than the value it wraps, which does not allow for a data() member method.
Instead my StaticVector< T, N, A > holds an array of unions, and uses the size() member method to keep track of all active union members in the array using the following class invariants:
The elements at [0, size()) have the value_type;
The elements at [size(), capacity()) have a trivial dummy type.
Have you considered using an underlying array containing elements of union type for the
static_vector
implementation?Fwiiw I implemented my own
StaticVector< T, N, A >
with the following goals:constexpr
expressions (unlike EASTL'seastl::fixed_vector
);operator new
/dynamic memory allocation to support compile-time initialization and runtime usage (unlike C++20'sstd::vector
);std::array
/C-style array).I was inspired by
std::optional< T >
to achieve the latter.One cannot use an array of
std::optional< T >
elements directly, because besides wrapping aunion
,std::optional< T >
needs to keep track of the activeunion
member as well. This bookkeeping is redundant for each array element. Furthermore,std::optional< T >
has a larger size than the value it wraps, which does not allow for adata()
member method.Instead my
StaticVector< T, N, A >
holds an array of unions, and uses thesize()
member method to keep track of all activeunion
members in the array using the following class invariants:size()
) have thevalue_type
;size()
,capacity()
) have a trivial dummy type.A slightly stripped down version can be found at https://developercommunity.visualstudio.com/t/failure-was-caused-by-attempting-to-acce/1515298 (MSVC has some issue(s) unlike clang and gcc).