cpp_result f19982d
Loading...
Searching...
No Matches

Modern Rust-like Result<T, E> for C++17.

cpp_result is a header-only, ergonomic Result type for C++17 inspired by Rust, with combinators, helpers, and void specialization.

  • See the API reference for details.
  • Quickstart:
    #include <result.hpp>
    struct Error {
    std::string message;
    };
    Result divide(int a, int b) {
    if (b == 0)
    return Result::Err({"Division by zero"});
    return Result::Ok(a / b);
    }
    int main() {
    auto r = divide(10, 2);
    if (r.is_ok())
    std::cout << r.unwrap() << '\n';
    else
    std::cout << r.unwrap_err().message << '\n';
    }
    Result<T, E> - Holds either a value (Ok) or an error (Err).
    Definition result.hpp:268

Feature Control

cpp_result provides fine-grained feature toggling via preprocessor macros. This allows you to reduce binary size or limit API surface by disabling unused features at compile time.

  • The main switch is CPP_RESULT_FEATURE_ALL (default: enabled). If set to 1, all features are enabled unless individually overridden. If set to 0, all features are disabled unless individually enabled.
  • Individual features:
    • CPP_RESULT_FEATURE_UNWRAP : Unwrap/expect helpers (unwrap, unwrap_err, expect, etc.)
    • CPP_RESULT_FEATURE_MAP : Map/map_err/map_or/map_or_else
    • CPP_RESULT_FEATURE_ANDOR : and_, and_then, or_, or_else
    • CPP_RESULT_FEATURE_INSPECT : inspect, inspect_err
    • CPP_RESULT_FEATURE_CONTAINS : contains, contains_err
    • CPP_RESULT_FEATURE_FLATTEN : flatten
    • CPP_RESULT_FEATURE_OPTIONAL : ok(), err() as std::optional

You can set these macros via your build system or before including result.hpp:

#define CPP_RESULT_FEATURE_ALL 0
#define CPP_RESULT_FEATURE_UNWRAP 1
#include <result.hpp>

If you use CMake or Meson, options are provided to control these features.

See also
README.md for more details.

Macros: TRY and TRYL

cpp_result provides two macros to simplify error propagation in functions returning Result types:

  • TRY(expr): Evaluates expr (which must return a Result). If it is an error, returns the error from the current function. Otherwise, yields the value.
  • TRYL(name, expr): Like TRY, but binds the unwrapped value to the variable name.

These macros mimic Rust's ? operator for early returns on error.

Example usage:

Result<int, std::string> parse_and_add(const std::string& a, const std::string& b) {
int x = TRY(parse_int(a));
int y = TRY(parse_int(b));
return Ok<int, std::string>(x + y);
}
// With TRYL:
Result<int, std::string> parse_and_add(const std::string& a, const std::string& b) {
TRYL(x, parse_int(a));
TRYL(y, parse_int(b));
return Ok<int, std::string>(x + y);
}
Result< T, E > Ok(T val)
Definition result.hpp:1072
#define TRYL(name, expr)
Propagate errors and bind the value to a variable.
Definition result.hpp:229
#define TRY(expr)
Propagate errors like Rust's ? operator.
Definition result.hpp:191
Note
These macros require the function to return a compatible Result type.