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

Result<void, E> - Specialization for operations that return no value, only success or error. More...

#include <result.hpp>

Public Member Functions

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

Static Public Member Functions

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

Detailed Description

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

Result<void, E> - Specialization for operations that return no value, only success or error.

Template Parameters
EError type

Example:

auto r = Ok<std::string>();
if (r.is_ok())
std::cout << "All good!";
else
std::cout << r.unwrap_err();
Result< void, E > Ok()
Definition result.hpp:1078

Constructor & Destructor Documentation

◆ Result() [1/4]

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

◆ Result() [2/4]

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

◆ ~Result()

cpp_result::Result< void, E >::~Result ( )
inline

◆ Result() [3/4]

cpp_result::Result< void, E >::Result ( Result< void, E > && other)
inlinenoexcept

◆ Result() [4/4]

cpp_result::Result< void, E >::Result ( const Result< void, E > & other)
inlinenoexcept

Member Function Documentation

◆ and_() [1/2]

auto cpp_result::Result< void, 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_() [2/2]

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

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

auto err = Err<std::string>("fail");
auto out = ok.and_(err);
assert(out.is_err());
std::optional< E > err() const
Converts the Result into a std::optional<E> (Err value or std::nullopt).
Definition result.hpp:957
std::optional< void > ok() const
Definition result.hpp:655
static Result Err(E err) noexcept
Construct an Err result.
Definition result.hpp:297

◆ and_then() [1/2]

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

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

auto r = Ok<std::string>();
auto chained = r.and_then([]{
});
assert(chained.unwrap() == 42);

◆ and_then() [2/2]

R cpp_result::Result< void, 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()

bool cpp_result::Result< void, E >::contains ( const void & 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() [1/2]

bool cpp_result::Result< void, 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"));

◆ contains_err() [2/2]

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

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

auto err = Err<std::string>("fail");
assert(err.contains_err("fail"));

◆ Err() [1/2]

static Result cpp_result::Result< void, E >::Err ( E err)
inlinestaticnoexcept

Construct an Err result.

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

◆ Err() [2/2]

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

Construct an Err result.

Parameters
errError to store
Returns
Err result
auto r = Err<std::string>("fail");

◆ err() [1/2]

std::optional< E > cpp_result::Result< void, 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

◆ err() [2/2]

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

Converts the Result into a std::optional<E> (Err value or std::nullopt).

auto r = Err<std::string>("fail");
std::optional<std::string> opt = r.err();
assert(opt.has_value());
assert(opt.value() == "fail");

◆ expect() [1/3]

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

◆ expect() [2/3]

void & cpp_result::Result< void, 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() [3/3]

template<typename E>
void cpp_result::Result< void, E >::expect ( const char msg[]) const
inlinenoexcept

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

auto r = Ok<st::string>();
r.expect("should not fail");

◆ expect_err() [1/4]

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

◆ expect_err() [2/4]

E & cpp_result::Result< void, 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");

◆ expect_err() [3/4]

template<typename E>
const E & cpp_result::Result< void, E >::expect_err ( const char msg[]) const
inlinenoexcept

◆ expect_err() [4/4]

template<typename E>
E & cpp_result::Result< void, E >::expect_err ( const char msg[])
inlinenoexcept

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

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

◆ flatten()

auto cpp_result::Result< void, 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() noexcept
Construct an Ok result.
Definition result.hpp:770

◆ inspect() [1/2]

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

Calls func() if Ok, returns self.

auto r = Ok<std::string>();
r.inspect([]{ std::cout << "All good!"; });

◆ inspect() [2/2]

const Result & cpp_result::Result< void, 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() [1/2]

const Result & cpp_result::Result< void, 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; });

◆ inspect_err() [2/2]

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

Calls func(error) if Err, returns self.

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

◆ is_err() [1/2]

bool cpp_result::Result< void, 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() [2/2]

template<typename E>
bool cpp_result::Result< void, E >::is_err ( ) const
inlinenoexcept

Returns true if the result is Err.

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

◆ is_err_and() [1/2]

bool cpp_result::Result< void, 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_err_and() [2/2]

template<typename E>
template<typename Pred>
bool cpp_result::Result< void, 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<std::string>("fail");
bool b = r.is_err_and([](const std::string& e){ return e == "fail"; });
assert(b);

◆ is_ok() [1/2]

bool cpp_result::Result< void, E >::is_ok ( ) const
inlineconstexprnoexcept

Returns true if the result is Ok.

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

◆ is_ok() [2/2]

template<typename E>
bool cpp_result::Result< void, E >::is_ok ( ) const
inlinenoexcept

Returns true if the result is Ok.

auto r = Ok<std::string>();
if (r.is_ok()) { ... }

◆ is_ok_and()

bool cpp_result::Result< void, 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() [1/2]

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

Returns unit type if Ok, else returns error.

auto r = Ok<std::string>();
auto mapped = r.map([]{ return 42; });
assert(mapped.unwrap() == 42);

◆ map() [2/2]

Result< U, E > cpp_result::Result< void, 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() [1/2]

Result< void, E2 > cpp_result::Result< void, 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_err() [2/2]

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

Maps the error if Err, else propagates Ok.

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

◆ map_or() [1/2]

U cpp_result::Result< void, 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() [2/2]

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

Applies a function if Ok, else returns default_value.

int v = ok.map_or(0, []{ return 42; });
assert(v == 42);

◆ map_or_else() [1/2]

auto cpp_result::Result< void, 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);

◆ map_or_else() [2/2]

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

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

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

◆ Ok() [1/2]

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

Construct an Ok result.

Returns
Ok result
auto r = Ok<std::string>();

◆ Ok() [2/2]

static Result cpp_result::Result< void, E >::Ok ( void val)
inlinestaticnoexcept

Construct an Ok result.

Parameters
valValue to store
Returns
Ok result
// or
auto r2 = Result::Ok(42);
static Result Ok(T val) noexcept
Construct an Ok result.
Definition result.hpp:284

◆ ok()

std::optional< void > cpp_result::Result< void, 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);

◆ operator=() [1/4]

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

◆ operator=() [2/4]

Result & cpp_result::Result< void, E >::operator= ( const Result< void, E > & other)
inlinenoexcept

◆ operator=() [3/4]

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

◆ operator=() [4/4]

Result & cpp_result::Result< void, E >::operator= ( Result< void, E > && other)
inlinenoexcept

◆ or_() [1/2]

auto cpp_result::Result< void, 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_() [2/2]

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

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

auto err = Err<std::string>("fail");
auto out = err.or_(ok);
assert(out.is_ok());

◆ or_else()

auto cpp_result::Result< void, 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/3]

const void & cpp_result::Result< void, E >::unwrap ( ) const
inlinenoexcept

◆ unwrap() [2/3]

void & cpp_result::Result< void, E >::unwrap ( )
inlinenoexcept

Unwraps the value. Aborts if Err.

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

◆ unwrap() [3/3]

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

Unwraps the value. Aborts if Err.

auto r = Ok<std::string>();
r.unwrap(); // does nothing if Ok

◆ unwrap_err() [1/4]

const E & cpp_result::Result< void, E >::unwrap_err ( ) const
inlinenoexcept

◆ unwrap_err() [2/4]

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

◆ unwrap_err() [3/4]

E & cpp_result::Result< void, 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_err() [4/4]

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

Unwraps the error. Aborts if Ok.

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

◆ unwrap_or()

void cpp_result::Result< void, E >::unwrap_or ( void 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()

void cpp_result::Result< void, 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()

void cpp_result::Result< void, 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: