Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/mguludag/expected

An extended version of C++23's std::expected<T, E> to use with variadic errors like expected<T, E1, E2, ...>
https://github.com/mguludag/expected

cpp17 cpp20 cpp23 cpp26 error-handling expected

Last synced: 3 days ago
JSON representation

An extended version of C++23's std::expected<T, E> to use with variadic errors like expected<T, E1, E2, ...>

Awesome Lists containing this project

README

        

# expected

An extended version of C++23's `std::expected` to handle variadic errors, allowing usage like `expected`.

## Table of Contents
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Basic Example](#basic-example)
- [Advanced Example](#advanced-example)
- [Contributing](#contributing)
- [License](#license)

## Features
- Compatible with C++17
- Supports multiple error types.
- Enhances standard `std::expected` functionality.

## Installation
Clone the repository:
```sh
git clone https://github.com/mguludag/expected.git
```
Include the `include` directory in your project.

## [Usage (click to demo)](https://godbolt.org/z/jonM4nf3b)

### Basic Example
Here's a simple example of how to use `expected` with multiple error types:
```cpp
#include

mgutility::expected divide(int a, int b) {
if (b == 0) return mgutility::unexpected{"Division by zero"};
return a / b;
}

int main() {
auto result = divide(4, 2);
if (!result) {
// Handle error
}
// Use result
}
```

### Advanced Example

#### Using `.and_then` for success and `.or_else` per Error Type and using `.transform`
* Chain operations that depend on the success of the previous one, and handle specific errors with `or_else`
* Transform the value inside `expected` if it is present
```cpp
#include
#include
#include
#include

mgutility::expected divide(int a, int b) {
if (b == 0) return mgutility::unexpected("Division by zero");
if (a < 0) return mgutility::unexpected{std::make_error_code(std::errc::invalid_argument)};
return a / b;
}

mgutility::expected addOne(int x) {
return x + 1;
}

int main() {
auto result = divide(10, 2)
.and_then(addOne)
.transform([](int x) { return x * 2; })
.or_else([](const std::string& err) -> mgutility::expected {
std::cerr << "String error: " << err << std::endl;
return mgutility::unexpected("Handled string error");
})
.or_else([](const std::error_code& err) -> mgutility::expected {
std::cerr << "Error code: " << err.message() << std::endl;
return mgutility::unexpected(err.message());
});

if (result) {
std::cout << "Result: " << *result << std::endl;
} else {
// use result.error() or result.error()
std::cerr << "Error: " << result.error() << std::endl;
}
}

```

## Contributing
Contributions are welcome! Please open issues or pull requests.

## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

This `README.md` provides a complete guide with basic and advanced usage examples, including multiple error types and chained handling using monadic operations.