Skip to content

result is a C++ library that provides a Result type, which can be used to return and propagate errors. It's inspired by Rust's Result type.

License

Notifications You must be signed in to change notification settings

GregoryKogan/result

Repository files navigation

result

GitHub License Github Tests workflow Github Tests workflow

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.

Documentation

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.

Features

  • Result<T, E> type for returning and propagating errors.
  • Ok and Err static methods for creating successful and unsuccessful Result objects respectively.
  • is_ok and is_err methods for checking if the Result is successful or unsuccessful.
  • unwrap, unwrap_or and unwrap_err methods for extracting the value or error from the Result.

Usage

#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

Contributing

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.

About

result is a C++ library that provides a Result type, which can be used to return and propagate errors. It's inspired by Rust's Result type.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published