#include <cstdio>
#include <cstdlib>
#include <optional>
#include <type_traits>
#include <utility>
Go to the source code of this file.
◆ CPP_RESULT_FEATURE_ALL
#define CPP_RESULT_FEATURE_ALL 1 |
◆ CPP_RESULT_FEATURE_ANDOR
◆ CPP_RESULT_FEATURE_CONTAINS
◆ CPP_RESULT_FEATURE_FLATTEN
◆ CPP_RESULT_FEATURE_INSPECT
◆ CPP_RESULT_FEATURE_MAP
◆ CPP_RESULT_FEATURE_OPTIONAL
◆ CPP_RESULT_FEATURE_UNWRAP
◆ EXPECT_OR_ABORT
#define EXPECT_OR_ABORT |
( |
| cond, |
|
|
| msg ) |
Value: do { \
if (!(cond)) { \
std::fputs(msg, stderr); \
std::fputc('\n', stderr); \
std::abort(); \
} \
} while (0)
◆ TRY
Value: ({ \
auto &&__res = (expr); \
using __res_type = std::decay_t<decltype(__res)>; \
if (__res.is_err()) \
return __res_type::Err(__res.unwrap_err()); \
std::move(__res.unwrap()); \
})
Propagate errors like Rust's ?
operator.
Evaluates expr
(which must return a Result). If it is an error, returns the error from the current function. Otherwise, yields the value.
- Note
- This implementation is less optimal if your compiler does not support statement expressions. It uses a lambda, which may result in less efficient code and different scoping rules.
- See also
- Source code for the macro definition.
Example:
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);
}
#define TRY(expr)
Propagate errors like Rust's ? operator.
Definition result.hpp:191
◆ TRYL
#define TRYL |
( |
| name, |
|
|
| expr ) |
Value: auto &&__res_##name = (expr); \
using __res_type_##name = std::decay_t<decltype(__res_##name)>; \
if (__res_##name.is_err()) \
return __res_type_##name::Err(__res_##name.unwrap_err()); \
auto &name = __res_##name.unwrap()
Propagate errors and bind the value to a variable.
Evaluates expr
(which must return a Result). If it is an error, returns the error from the current function. Otherwise, binds the unwrapped value to the variable name
.
Example:
Result<int, std::string> parse_and_add(const std::string& a, const std::string& b) {
return Ok<int, std::string>(x + y);
}
#define TRYL(name, expr)
Propagate errors and bind the value to a variable.
Definition result.hpp:229