gnzlbg / static_vector

A dynamically-resizable vector with fixed capacity and embedded storage
https://gnzlbg.github.io/static_vector
167 stars 21 forks source link

[Question]: array of elements of union type #54

Open matt77hias opened 3 years ago

matt77hias commented 3 years ago

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:

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:

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).