sumty
0.1.0
Better sum types for C++
|
sumty
is a header-only C++20 library that provides "better" sum types. The C++ standard library provides std::variant
, std::optional
, and std::exepcted
(as of C++23), but these have some limitiations that sumty
aims to remove.
sumty
provides four sum types, 3 of which correspond to one of the STL types mentioned above.
std::variant
std::optional
std::expected
std::variant
These sum types differ from those in the STL in a number of ways. Primarily, sumty
's sum types extend alternatives to support references (lvalue and rvalue) and void
. sumty
also guarantees a few size optimizations that applies to all sum types, the boil down to taking advantage of empty types and the invariant that lvalue references are not null. Some size optimization examples are listed below.
sizeof(variant<std::monostate, std::monostate>) == sizeof(bool)
sizeof(option<int&>) == sizeof(void*)
sizeof(result<void, void>) == sizeof(bool)
sizeof(error_set<empty1, empty2, empty3>) == sizeof(uint8_t)