Result-CPP is a C++ library that provides a Result<T, E> type, which can be used to return and propagate errors. It's inspired by Rust's std::Result type.
It's a header-only library, so you can just copy the result.h file into your project and start using it.
Or you can use it as a git submodule.
Or fetch it with FetchContent if you're using CMake.
You can find the documentation here.
And the project's repo here.
It's auto generated with Doxygen by GitHub Actions and hosted on GitHub Pages. It uses the Doxygen Awesome theme, that I customized to use Tokyo Night palette.
Result<T, E>type for returning and propagating errors.OkandErrstatic methods for creating successful and unsuccessfulResultobjects respectively.is_okandis_errmethods for checking if theResultis successful or unsuccessful.unwrap,unwrap_orandunwrap_errmethods for extracting the value or error from theResult.
#include <iostream>
#include "result.h"
res::Result<int, std::string> divide(int a, int b) {
if (b == 0) {
return res::Err(std::string("Division by zero"));
} else {
return res::Ok(a / b);
}
}
int main() {
auto result = divide(10, 2);
if (result.is_ok()) {
std::cout << "Result: " << result.unwrap() << '\n';
} else {
std::cout << "Error: " << result.unwrap_err() << '\n';
}
}You can find many other use cases within /tests directory
I'm not planning to write any more features for this library, but I will gladly accept any pull requests that add new features or fix bugs.