A C++17 header-only drop-in replacement for std::optional that eliminates memory waste for certain types using unused bit patterns or sentinel values.
tiny::optional is a C++ library that serves as a memory-efficient alternative to std::optional. It solves the problem of std::optional's memory waste by storing the empty state within the payload itself using unused bit patterns or user-defined sentinel values, resulting in zero overhead for supported types.
C++ developers working on memory-sensitive applications, such as embedded systems, high-performance computing, or large-scale data processing, who need optional types without the memory bloat of std::optional.
Developers choose tiny::optional over std::optional because it provides identical functionality with significantly reduced memory usage for many common types, leading to better cache performance and lower memory footprint in critical applications.
Drop-in replacement for std::optional that does not waste memory unnecessarily
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
For types like bool, double, and pointers, sizeof(tiny::optional<T>) equals sizeof(T), eliminating padding waste compared to std::optional, as shown with static_assert examples in the README.
Allows defining sentinel values (e.g., -1 for int) to represent emptiness with no extra memory, providing type safety over raw sentinel usage, demonstrated with tiny::optional<int, -1>.
Via tiny::optional_flag_manipulator specialization, custom types can store the empty state in-place, enabling compression for user-defined types like IndexPair, as detailed in the README.
Requires only C++17 and works on clang, gcc, and MSVC across Windows, Linux, and macOS, with extensive testing shown via GitHub Actions badges.
Relies on exploiting undefined behavior on x86/x64 for zero-overhead storage, which risks portability and potential issues on other architectures, though a flag can disable tricks at the cost of features.
Specializing tiny::optional_flag_manipulator requires careful implementation and consistent visibility across translation units to avoid undefined behavior, with the README warning about subtle bugs and placement issues.
Methods are not constexpr due to reliance on std::memcpy in C++17, limiting compile-time usage, which the README admits as a drawback for consteval contexts.