|
cpp_result e4f6bfe
|
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.
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.
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.CPP_RESULT_FEATURE_UNWRAP : Unwrap/expect helpers (unwrap, unwrap_err, expect, etc.)CPP_RESULT_FEATURE_MAP : Map/map_err/map_or/map_or_elseCPP_RESULT_FEATURE_ANDOR : and_, and_then, or_, or_elseCPP_RESULT_FEATURE_INSPECT : inspect, inspect_errCPP_RESULT_FEATURE_CONTAINS : contains, contains_errCPP_RESULT_FEATURE_FLATTEN : flattenCPP_RESULT_FEATURE_OPTIONAL : ok(), err() as std::optionalYou can set these macros via your build system or before including result.hpp:
If you use CMake or Meson, options are provided to control these features.
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.TRY_ASSIGN(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:
TRY macro requires your compiler to support statement expressions.T and E must not be reference types.T and E must not be same types.