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: 11 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, ...>
- Host: GitHub
- URL: https://github.com/mguludag/expected
- Owner: mguludag
- License: mit
- Created: 2024-07-29T16:07:49.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-17T12:00:56.000Z (over 1 year ago)
- Last Synced: 2025-04-10T00:51:03.226Z (10 months ago)
- Topics: cpp17, cpp20, cpp23, cpp26, error-handling, expected
- Language: C++
- Homepage:
- Size: 179 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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.