An open API service indexing awesome lists of open source software.

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

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.