https://github.com/rvarago/cpp_optional_extras
*EXPERIMENTAL* A handful of C++ utilities I wish std::optional<T> had
https://github.com/rvarago/cpp_optional_extras
cpp cpp20-library functional-programming optional
Last synced: about 1 month ago
JSON representation
*EXPERIMENTAL* A handful of C++ utilities I wish std::optional<T> had
- Host: GitHub
- URL: https://github.com/rvarago/cpp_optional_extras
- Owner: rvarago
- License: mit
- Created: 2025-01-22T08:39:02.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-03-04T10:29:54.000Z (8 months ago)
- Last Synced: 2025-03-04T11:33:49.575Z (8 months ago)
- Topics: cpp, cpp20-library, functional-programming, optional
- Language: C++
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cpp_optional_extras
A handful of C++ utilities I wish [`std::optional`](https://en.cppreference.com/w/cpp/utility/optional) had.
## Purpose
C++17 brought us `std::optional`, then C++23 enhanced it with a couple of member-functions (e.g. `transform`) to safely express common data transformations.
This library draws inspiration from other languages and provides a handful of additional functions to make working with `std::optional` even more ergonomic and less error-prone by encapsulating common patterns into reusable algorithms around correct access to the underlying `T`.
### Examples
You have an function `get_access_token` that returns an `access_token` and you want to `verify_signature` and do something when it's valid. With `exists`:
```cpp
auto get_access_token() -> std::optional;
auto verify_signature(access_token const &) -> bool;
if (optx::exists(get_access_token(), verify_signature)) {
// do something
}
```
You have a `person` with optional `first_name` and `second_name`, and you want to format them when both are available or return the first available one, otherwise return `N/A` when neither is available. With `append`:
```cpp
struct person {
std::optional first_name;
std::optional second_name;
auto format_name() const -> std::string {
return optx::append(first_name, second_name,
[](auto const fname, auto const sname) {
return fname + " " + sname;
})
.value_or(std::string{"N/A"});
}
};
```
These are just two of the available algorithms!
### Functions
- `filter(optional, T -> bool) -> optional`
- `exists(optional, T -> bool) -> bool`
- `contains(optional, T) -> bool`
- `zip_with(optional, optional, (L, R) -> O) -> optional`
- `zip(optional, optional) -> optional>`
- `unzip(optional) -> pair, optional>`
- `append(optional, optional, (T, T) -> T) -> optional`
- `fold(optional, () -> R, T -> R) -> R`
## Requirements
C++20
## Usage
This is a header-only library. Algorithms live in [`algorithm.hpp`](include/rvarago/optional_extras/algorithm.hpp).
- (Optional) Link against the INTERFACE `rvarago::optional_extras` target in your CMake build.
- Include `rvarago/optional_extras/algorithm.hpp`
- Call into functions from the `rvarago::optional_extras::algorithm` namespace, perhaps alias it as `namespace optx = rvarago::optional_extras::algorithm;` to save keystrokes.
## Contributing
This repository has a [`flake.nix`](./flake.nix) with everything I need for development/CI (toolchain, language server, etc).
Furthermore, with:
```sh
nix develop -c check
```
You run all CI checks locally.