cpp_result f19982d
Loading...
Searching...
No Matches
result.hpp File Reference
#include <cstdio>
#include <cstdlib>
#include <optional>
#include <type_traits>
#include <utility>

Go to the source code of this file.

Classes

class  cpp_result::Result< T, E >
 Result<T, E> - Holds either a value (Ok) or an error (Err). More...
 
class  cpp_result::Result< void, E >
 Result<void, E> - Specialization for operations that return no value, only success or error. More...
 

Namespaces

namespace  cpp_result
 

Macros

#define CPP_RESULT_FEATURE_ALL   1
 
#define CPP_RESULT_FEATURE_UNWRAP   CPP_RESULT_FEATURE_ALL
 
#define CPP_RESULT_FEATURE_MAP   CPP_RESULT_FEATURE_ALL
 
#define CPP_RESULT_FEATURE_ANDOR   CPP_RESULT_FEATURE_ALL
 
#define CPP_RESULT_FEATURE_INSPECT   CPP_RESULT_FEATURE_ALL
 
#define CPP_RESULT_FEATURE_CONTAINS   CPP_RESULT_FEATURE_ALL
 
#define CPP_RESULT_FEATURE_FLATTEN   CPP_RESULT_FEATURE_ALL
 
#define CPP_RESULT_FEATURE_OPTIONAL   CPP_RESULT_FEATURE_ALL
 
#define TRY(expr)
 Propagate errors like Rust's ? operator.
 
#define TRYL(name, expr)
 Propagate errors and bind the value to a variable.
 
#define EXPECT_OR_ABORT(cond, msg)
 

Functions

template<typename T, typename E>
Result< T, E > cpp_result::Ok (T val)
 
template<typename T, typename E>
Result< T, E > cpp_result::Err (E err)
 
template<typename E>
Result< void, E > cpp_result::Ok ()
 
template<typename E>
Result< void, E > cpp_result::Err (E err)
 

Macro Definition Documentation

◆ CPP_RESULT_FEATURE_ALL

#define CPP_RESULT_FEATURE_ALL   1

◆ CPP_RESULT_FEATURE_ANDOR

#define CPP_RESULT_FEATURE_ANDOR   CPP_RESULT_FEATURE_ALL

◆ CPP_RESULT_FEATURE_CONTAINS

#define CPP_RESULT_FEATURE_CONTAINS   CPP_RESULT_FEATURE_ALL

◆ CPP_RESULT_FEATURE_FLATTEN

#define CPP_RESULT_FEATURE_FLATTEN   CPP_RESULT_FEATURE_ALL

◆ CPP_RESULT_FEATURE_INSPECT

#define CPP_RESULT_FEATURE_INSPECT   CPP_RESULT_FEATURE_ALL

◆ CPP_RESULT_FEATURE_MAP

#define CPP_RESULT_FEATURE_MAP   CPP_RESULT_FEATURE_ALL

◆ CPP_RESULT_FEATURE_OPTIONAL

#define CPP_RESULT_FEATURE_OPTIONAL   CPP_RESULT_FEATURE_ALL

◆ CPP_RESULT_FEATURE_UNWRAP

#define CPP_RESULT_FEATURE_UNWRAP   CPP_RESULT_FEATURE_ALL

◆ 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

#define TRY ( expr)
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) {
TRYL(x, parse_int(a));
TRYL(y, parse_int(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