cpp_result f19982d
Loading...
Searching...
No Matches
cpp_result::Result< T, E > Class Template Reference

Result<T, E> - Holds either a value (Ok) or an error (Err). More...

#include <result.hpp>

Public Member Functions

constexpr bool is_ok () const noexcept
 Returns true if the result is Ok.
 
constexpr bool is_err () const noexcept
 Returns true if the result is Err.
 
T & unwrap () noexcept
 Unwraps the value. Aborts if Err.
 
const T & unwrap () const noexcept
 
E & unwrap_err () noexcept
 Unwraps the error. Aborts if Ok.
 
const E & unwrap_err () const noexcept
 
unwrap_or (T default_value) const noexcept
 Returns value if Ok, else returns default_value.
 
template<typename F>
unwrap_or_else (F &&func) const noexcept(noexcept(func()))
 Returns value if Ok, else calls func().
 
unwrap_or_default () const noexcept(std::is_nothrow_default_constructible_v< T >)
 Returns the value if Ok, else returns a default-constructed value. Requires T to be default-constructible.
 
T & expect (const char *msg) noexcept
 Unwraps the value or aborts with a custom message if Err.
 
const T & expect (const char *msg) const noexcept
 
E & expect_err (const char *msg) noexcept
 Unwraps the error or aborts with a custom message if Ok.
 
const E & expect_err (const char *msg) const noexcept
 
template<typename Pred>
bool is_ok_and (Pred &&pred) const noexcept(noexcept(pred(std::declval< T >())))
 Returns true if the result is Ok and the predicate returns true for the value.
 
template<typename Pred>
bool is_err_and (Pred &&pred) const noexcept(noexcept(pred(std::declval< E >())))
 Returns true if the result is Err and the predicate returns true for the error.
 
template<typename F, typename U = std::invoke_result_t<F, T>>
Result< U, E > map (F &&func) const noexcept(noexcept(func(std::declval< T >())))
 Maps the value if Ok, else propagates Err.
 
template<typename F, typename E2 = std::invoke_result_t<F, E>>
Result< T, E2 > map_err (F &&func) const noexcept(noexcept(func(std::declval< E >())))
 Maps the error if Err, else propagates Ok.
 
template<typename U, typename F>
map_or (U default_value, F &&func) const
 Applies a function to the value if Ok, else returns default_value.
 
template<typename D, typename F>
auto map_or_else (D &&default_fn, F &&func) const
 Applies a function to the value if Ok, else computes a default with another function.
 
template<typename F, typename R = typename std::invoke_result_t<F, T>>
and_then (F &&func) const noexcept(noexcept(func(std::declval< T >())))
 Chains another result-producing function if Ok, else propagates Err.
 
template<typename R2>
auto and_ (R2 &&res) const
 Returns res if the result is Ok, otherwise returns self.
 
template<typename R2>
auto or_ (R2 &&res) const
 Returns res if the result is Err, otherwise returns self.
 
template<typename F>
auto or_else (F &&op) const
 Calls op if the result is Err, otherwise returns self.
 
template<typename F>
const Resultinspect (F &&func) const noexcept(noexcept(func(std::declval< const T & >())))
 Calls func(value) if Ok, returns self.
 
template<typename F>
const Resultinspect_err (F &&func) const noexcept(noexcept(func(std::declval< const E & >())))
 Calls func(error) if Err, returns self.
 
bool contains (const T &value) const
 Returns true if the result is Ok and contains the given value.
 
bool contains_err (const E &error) const
 Returns true if the result is Err and contains the given error.
 
auto flatten () const
 Flattens a Result<Result<U, E>, E> into Result<U, E>.
 
std::optional< T > ok () const
 Returns the value as std::optional if Ok, otherwise std::nullopt.
 
std::optional< E > err () const
 Returns the error as std::optional if Err, otherwise std::nullopt.
 
 ~Result ()
 
 Result (Result &&other) noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_constructible_v< E >)
 
Resultoperator= (Result &&other) noexcept(std::is_nothrow_move_assignable_v< T > &&std::is_nothrow_move_assignable_v< E >)
 
 Result (const Result &other) noexcept(std::is_nothrow_copy_constructible_v< T > &&std::is_nothrow_copy_constructible_v< E >)
 
Resultoperator= (const Result &other) noexcept(std::is_nothrow_copy_assignable_v< T > &&std::is_nothrow_copy_assignable_v< E >)
 

Static Public Member Functions

static Result Ok (T val) noexcept
 Construct an Ok result.
 
static Result Err (E err) noexcept
 Construct an Err result.
 

Detailed Description

template<typename T, typename E>
class cpp_result::Result< T, E >

Result<T, E> - Holds either a value (Ok) or an error (Err).

Template Parameters
TValue type
EError type

Example:

auto r = Ok<int, std::string>(42);
if (r.is_ok())
std::cout << r.unwrap();
Result< void, E > Ok()
Definition result.hpp:1078

Constructor & Destructor Documentation

◆ ~Result()

template<typename T, typename E>
cpp_result::Result< T, E >::~Result ( )
inline

◆ Result() [1/2]

template<typename T, typename E>
cpp_result::Result< T, E >::Result ( Result< T, E > && other)
inlinenoexcept

◆ Result() [2/2]

template<typename T, typename E>
cpp_result::Result< T, E >::Result ( const Result< T, E > & other)
inlinenoexcept

Member Function Documentation

◆ and_()

template<typename T, typename E>
template<typename R2>
auto cpp_result::Result< T, E >::and_ ( R2 && res) const
inline

Returns res if the result is Ok, otherwise returns self.

auto r1 = Ok<int, std::string>(1);
auto r2 = Ok<int, std::string>(2);
auto out = r1.and_(r2);
assert(out.unwrap() == 2);

◆ and_then()

template<typename T, typename E>
template<typename F, typename R = typename std::invoke_result_t<F, T>>
R cpp_result::Result< T, E >::and_then ( F && func) const
inlinenoexcept

Chains another result-producing function if Ok, else propagates Err.

auto chained = r.and_then([](int v){
return Ok<int, std::string>(v+1);
});
assert(chained.unwrap() == 2);

◆ contains()

template<typename T, typename E>
bool cpp_result::Result< T, E >::contains ( const T & value) const
inline

Returns true if the result is Ok and contains the given value.

auto r = Ok<int, std::string>(42);
assert(r.contains(42));

◆ contains_err()

template<typename T, typename E>
bool cpp_result::Result< T, E >::contains_err ( const E & error) const
inline

Returns true if the result is Err and contains the given error.

auto r = Err<int, std::string>("fail");
assert(r.contains_err("fail"));
static Result Err(E err) noexcept
Construct an Err result.
Definition result.hpp:297

◆ Err()

template<typename T, typename E>
static Result cpp_result::Result< T, E >::Err ( E err)
inlinestaticnoexcept

Construct an Err result.

Parameters
errError to store
Returns
Err result
auto r = Result::Err("fail");
// or
Result<T, E> - Holds either a value (Ok) or an error (Err).
Definition result.hpp:268
Result(Result &&other) noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_move_constructible_v< E >)
Definition result.hpp:675
Result< T, E > Err(E err)
Definition result.hpp:1075

◆ err()

template<typename T, typename E>
std::optional< E > cpp_result::Result< T, E >::err ( ) const
inline

Returns the error as std::optional if Err, otherwise std::nullopt.

std::optional<std::string> opt = r.err();
assert(opt.has_value() && *opt == "fail");
std::optional< E > err() const
Returns the error as std::optional if Err, otherwise std::nullopt.
Definition result.hpp:667

◆ expect() [1/2]

template<typename T, typename E>
const T & cpp_result::Result< T, E >::expect ( const char * msg) const
inlinenoexcept

◆ expect() [2/2]

template<typename T, typename E>
T & cpp_result::Result< T, E >::expect ( const char * msg)
inlinenoexcept

Unwraps the value or aborts with a custom message if Err.

auto r = Ok<int, std::string>(42);
assert(r.expect("should not fail") == 42);

◆ expect_err() [1/2]

template<typename T, typename E>
const E & cpp_result::Result< T, E >::expect_err ( const char * msg) const
inlinenoexcept

◆ expect_err() [2/2]

template<typename T, typename E>
E & cpp_result::Result< T, E >::expect_err ( const char * msg)
inlinenoexcept

Unwraps the error or aborts with a custom message if Ok.

auto r = Err<int, std::string>("fail");
assert(r.expect_err("should not fail") == "fail");

◆ flatten()

template<typename T, typename E>
auto cpp_result::Result< T, E >::flatten ( ) const
inline

Flattens a Result<Result<U, E>, E> into Result<U, E>.

auto inner = Ok<int, std::string>(42);
auto outer = Ok<Result<int, std::string>, std::string>(inner);
assert(outer.flatten().unwrap() == 42);
static Result Ok(T val) noexcept
Construct an Ok result.
Definition result.hpp:284

◆ inspect()

template<typename T, typename E>
template<typename F>
const Result & cpp_result::Result< T, E >::inspect ( F && func) const
inlinenoexcept

Calls func(value) if Ok, returns self.

auto r = Ok<int, std::string>(42);
r.inspect([](int v){ std::cout << v; });

◆ inspect_err()

template<typename T, typename E>
template<typename F>
const Result & cpp_result::Result< T, E >::inspect_err ( F && func) const
inlinenoexcept

Calls func(error) if Err, returns self.

auto r = Err<int, std::string>("fail");
r.inspect_err([](const std::string& e){ std::cout << e; });

◆ is_err()

template<typename T, typename E>
bool cpp_result::Result< T, E >::is_err ( ) const
inlineconstexprnoexcept

Returns true if the result is Err.

auto r = Err<int, std::string>("fail");
if (r.is_err()) { ... }

◆ is_err_and()

template<typename T, typename E>
template<typename Pred>
bool cpp_result::Result< T, E >::is_err_and ( Pred && pred) const
inlinenoexcept

Returns true if the result is Err and the predicate returns true for the error.

auto r = Err<int std::string>("fail");
bool b = r.is_err_and([](const std::string& e){ return e == "fail"; });
assert(b);

◆ is_ok()

template<typename T, typename E>
bool cpp_result::Result< T, E >::is_ok ( ) const
inlineconstexprnoexcept

Returns true if the result is Ok.

if (r.is_ok()) { ... }

◆ is_ok_and()

template<typename T, typename E>
template<typename Pred>
bool cpp_result::Result< T, E >::is_ok_and ( Pred && pred) const
inlinenoexcept

Returns true if the result is Ok and the predicate returns true for the value.

auto r = Ok<int, std::string>(42);
bool b = r.is_ok_and([](int v){ return v > 0; });
assert(b);

◆ map()

template<typename T, typename E>
template<typename F, typename U = std::invoke_result_t<F, T>>
Result< U, E > cpp_result::Result< T, E >::map ( F && func) const
inlinenoexcept

Maps the value if Ok, else propagates Err.

auto r = Ok<int, std::string>(21);
auto mapped = r.map([](int v){ return v*2; });
assert(mapped.unwrap() == 42);

◆ map_err()

template<typename T, typename E>
template<typename F, typename E2 = std::invoke_result_t<F, E>>
Result< T, E2 > cpp_result::Result< T, E >::map_err ( F && func) const
inlinenoexcept

Maps the error if Err, else propagates Ok.

auto r = Err<int, std::string>("fail");
auto mapped = r.map_err([](const std::string& e){ return e+"!"; });
assert(mapped.unwrap_err() == "fail!");

◆ map_or()

template<typename T, typename E>
template<typename U, typename F>
U cpp_result::Result< T, E >::map_or ( U default_value,
F && func ) const
inline

Applies a function to the value if Ok, else returns default_value.

auto r = Ok<int, std::string>(21);
int v = r.map_or(0, [](int v){ return v*2; });
assert(v == 42);

◆ map_or_else()

template<typename T, typename E>
template<typename D, typename F>
auto cpp_result::Result< T, E >::map_or_else ( D && default_fn,
F && func ) const
inline

Applies a function to the value if Ok, else computes a default with another function.

auto r = Err<int, std::string>("fail");
int v = r.map_or_else([]{ return 0; }, [](int v){ return v*2; });
assert(v == 0);

◆ Ok()

template<typename T, typename E>
static Result cpp_result::Result< T, E >::Ok ( T val)
inlinestaticnoexcept

Construct an Ok result.

Parameters
valValue to store
Returns
Ok result

◆ ok()

template<typename T, typename E>
std::optional< T > cpp_result::Result< T, E >::ok ( ) const
inline

Returns the value as std::optional if Ok, otherwise std::nullopt.

std::optional<int> opt = r.ok();
assert(opt.has_value() && *opt == 42);
std::optional< T > ok() const
Returns the value as std::optional if Ok, otherwise std::nullopt.
Definition result.hpp:655

◆ operator=() [1/2]

template<typename T, typename E>
Result & cpp_result::Result< T, E >::operator= ( const Result< T, E > & other)
inlinenoexcept

◆ operator=() [2/2]

template<typename T, typename E>
Result & cpp_result::Result< T, E >::operator= ( Result< T, E > && other)
inlinenoexcept

◆ or_()

template<typename T, typename E>
template<typename R2>
auto cpp_result::Result< T, E >::or_ ( R2 && res) const
inline

Returns res if the result is Err, otherwise returns self.

auto r1 = Err<int, std::string>("fail");
auto r2 = Ok<int, std::string>(2);
auto out = r1.or_(r2);
assert(out.unwrap() == 2);

◆ or_else()

template<typename T, typename E>
template<typename F>
auto cpp_result::Result< T, E >::or_else ( F && op) const
inline

Calls op if the result is Err, otherwise returns self.

auto r = Err<int, std::string>("fail");
auto out = r.or_else([]{ return Ok<int, std::string>(42); });
assert(out.unwrap() == 42);

◆ unwrap() [1/2]

template<typename T, typename E>
const T & cpp_result::Result< T, E >::unwrap ( ) const
inlinenoexcept

◆ unwrap() [2/2]

template<typename T, typename E>
T & cpp_result::Result< T, E >::unwrap ( )
inlinenoexcept

Unwraps the value. Aborts if Err.

Returns
T&
auto r = Ok<int, std::string>(42);
assert(r.unwrap() == 42);

◆ unwrap_err() [1/2]

template<typename T, typename E>
const E & cpp_result::Result< T, E >::unwrap_err ( ) const
inlinenoexcept

◆ unwrap_err() [2/2]

template<typename T, typename E>
E & cpp_result::Result< T, E >::unwrap_err ( )
inlinenoexcept

Unwraps the error. Aborts if Ok.

Returns
E&
auto r = Err<int, std::string>("fail");
assert(r.unwrap_err() == "fail");

◆ unwrap_or()

template<typename T, typename E>
T cpp_result::Result< T, E >::unwrap_or ( T default_value) const
inlinenoexcept

Returns value if Ok, else returns default_value.

auto r = Err<int, std::string>("fail");
int v = r.unwrap_or(123);
assert(v == 123);

◆ unwrap_or_default()

template<typename T, typename E>
T cpp_result::Result< T, E >::unwrap_or_default ( ) const
inlinenoexcept

Returns the value if Ok, else returns a default-constructed value. Requires T to be default-constructible.

auto r1 = Ok<int, std::string>(42);
assert(r1.unwrap_or_default() == 42);
auto r2 = Err<int, std::string>("fail");
assert(r2.unwrap_or_default() == 0); // int default

◆ unwrap_or_else()

template<typename T, typename E>
template<typename F>
T cpp_result::Result< T, E >::unwrap_or_else ( F && func) const
inlinenoexcept

Returns value if Ok, else calls func().

auto r = Err<int, std::string>("fail");
int v = r.unwrap_or_else([]{ return 123; });
assert(v == 123);

The documentation for this class was generated from the following file: