Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
matt77hias opened this issue Aug 27, 2021 · 0 comments
Open

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

matt77hias opened this issue Aug 27, 2021 · 0 comments

Comments

@matt77hias
Copy link

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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant