Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
arene::base::result< T, E > Class Template Reference

A sum type which can hold either a value or an error. Similar to std::expected. More...

Inheritance diagram for arene::base::result< T, E >:
Inheritance graph

Public Types

using error_type = E
 The type when the result contains an error.
 
using value_type = T
 The type when the result contains a value.
 

Public Member Functions

constexpr result ()=default
 Constructs a result with a default-constructed value_type . Does not participate in overload resolution if value_type is not default constructible.
 
template<class... P, constraints< std::enable_if_t< std::is_constructible< error_type, P &&... >::value > > = nullptr>
constexpr result (in_place_error_t, P &&... args) noexcept(noexcept(base_type(in_place_error, std::forward< P >(args)...)))
 Constructs a result containing the error_type by way of direct-non-list-initialization.
 
template<class... P, constraints< std::enable_if_t< std::is_constructible< value_type, P &&... >::value > > = nullptr>
constexpr result (in_place_value_t, P &&... args) noexcept(noexcept(base_type(in_place_value, std::forward< P >(args)...)))
 Constructs a result containing the value_type by way of direct-non-list-initialization.
 
constexpr result (result &&move) noexcept(noexcept(base_type(std::declval< result && >())))=default
 Default move constructor.
 
constexpr result (result const &copy) noexcept(noexcept(base_type(std::declval< result const & >())))=default
 Default copy constructor.
 
 ~result ()=default
 Destroy the stored value or error.
 
template<typename... ArgTypes, constraints< std::enable_if_t< std::is_nothrow_constructible< value_type, ArgTypes &&... >::value > > = nullptr>
auto emplace (in_place_value_t, ArgTypes &&... args) noexcept -> value_type &
 Emplaces a value into the result.
 
ARENE_NODISCARD constexpr auto error () &&noexcept -> error_type &&
 Accesses a reference to the content of the error channel.
 
ARENE_NODISCARD constexpr auto error () &noexcept -> error_type &
 Accesses a reference to the content of the error channel.
 
ARENE_NODISCARD constexpr auto error () const &&noexcept -> error_type const &&
 Accesses a reference to the content of the error channel.
 
ARENE_NODISCARD constexpr auto error () const &noexcept -> error_type const &
 Accesses a reference to the content of the error channel.
 
template<typename U = value_type, constraints< std::enable_if_t< is_equality_comparable_v< U > > > = nullptr>
ARENE_NODISCARD constexpr auto has_value (value_type const &val) const noexcept(noexcept(val==std::declval< value_type >())) -> bool
 Checks if the value channel contains a specific value.
 
ARENE_NODISCARD constexpr auto operator* () &&noexcept -> value_type &&
 Accesses a reference to the content of the value channel.
 
ARENE_NODISCARD constexpr auto operator* () &noexcept -> value_type &
 Accesses a reference to the content of the value channel.
 
ARENE_NODISCARD constexpr auto operator* () const &&noexcept -> value_type const &&
 Accesses a reference to the content of the value channel.
 
ARENE_NODISCARD constexpr auto operator* () const &noexcept -> value_type const &
 Accesses a reference to the content of the value channel.
 
ARENE_NODISCARD constexpr auto operator-> () const noexcept -> value_type const *
 Accesses a pointer to the content of the value channel.
 
ARENE_NODISCARD constexpr auto operator-> () noexcept -> value_type *
 Accesses a pointer to the content of the value channel.
 
constexpr auto operator= (copy_assign_arg const &other) noexcept -> result &
 Copy-assignment for the case that value_type and error_type satisfy std::is_nothrow_copy_constructible and std::is_nothrow_copy_assignable, otherwise a dummy assignment operator that can never be called.
 
constexpr auto operator= (deleted_copy_assign_arg const &other) noexcept -> result &=delete
 Deleted Copy-assignment if value_type or error_type do not satisfy std::is_nothrow_copy_constructible and std::is_nothrow_copy_assignable, otherwise a deleted dummy assignment operator.
 
constexpr auto operator= (deleted_move_assign_arg &&other) noexcept -> result &=delete
 Deleted Move-assignment if value_type or error_type do not satisfy std::is_nothrow_move_constructible and std::is_nothrow_move_assignable, otherwise a deleted dummy assignment operator.
 
constexpr auto operator= (move_assign_arg &&other) noexcept -> result &
 Move-assignment for the case that value_type and error_type satisfy std::is_nothrow_move_constructible and std::is_nothrow_move_assignable, otherwise a dummy assignment operator that can never be called.
 
template<typename SourceValueType = T, typename SourceErrorType = E, constraints< std::enable_if_t< is_swappable_v< SourceValueType > >, std::enable_if_t< is_swappable_v< SourceErrorType > >, std::enable_if_t< std::is_nothrow_move_constructible< SourceValueType >::value >, std::enable_if_t< std::is_nothrow_move_constructible< SourceErrorType >::value > > = nullptr>
constexpr void swap (result &other) noexcept(is_nothrow_swappable_v< SourceValueType > &&is_nothrow_swappable_v< SourceErrorType >)
 Swaps the state of two results.
 
ARENE_NODISCARD constexpr auto value () &&noexcept -> value_type &&
 Accesses a reference to the content of the value channel.
 
ARENE_NODISCARD constexpr auto value () &noexcept -> value_type &
 Accesses a reference to the content of the value channel.
 
ARENE_NODISCARD constexpr auto value () const &&noexcept -> value_type const &&
 Accesses a reference to the content of the value channel.
 
ARENE_NODISCARD constexpr auto value () const &noexcept -> value_type const &
 Accesses a reference to the content of the value channel.
 
template<typename U = T, constraints< std::enable_if_t< std::is_convertible< U, value_type >::value > > = nullptr>
ARENE_NODISCARD constexpr auto value_or (U &&default_value) &&noexcept -> value_type
 Returns the value if the value channel is populated, else another value. Not declared if T is void .
 
template<typename U = T, constraints< std::enable_if_t< std::is_convertible< U, value_type >::value > > = nullptr>
ARENE_NODISCARD constexpr auto value_or (U &&default_value) const &noexcept -> value_type
 Returns the value if the value channel is populated, else another value. Not declared if T is void .
 
template<typename F>
constexpr auto and_then (F &&handle_value) &
 Monadic API which invokes a functor with the contents of value() if the value channel is populated.
 
template<typename F>
constexpr auto and_then (F &&handle_value) const &
 Monadic API which invokes a functor with the contents of value() if the value channel is populated.
 
template<typename F>
constexpr auto and_then (F &&handle_value) &&
 Monadic API which invokes a functor with the contents of value() if the value channel is populated.
 
template<typename F>
constexpr auto and_then (F &&handle_value) const &&
 Monadic API which invokes a functor with the contents of value() if the value channel is populated.
 
template<typename F>
constexpr auto or_else (F &&handle_error) &
 Monadic API which invokes a functor with the contents of error() if the error channel is populated.
 
template<typename F>
constexpr auto or_else (F &&handle_error) const &
 Monadic API which invokes a functor with the contents of error() if the error channel is populated.
 
template<typename F>
constexpr auto or_else (F &&handle_error) &&
 Monadic API which invokes a functor with the contents of error() if the error channel is populated.
 
template<typename F>
constexpr auto or_else (F &&handle_error) const &&
 Monadic API which invokes a functor with the contents of error() if the error channel is populated.
 
template<typename F>
constexpr auto transform (F &&handle_value) &
 Calls a provided functor with the value channel if it is populated, and produces a new result with the return from it.
 
template<typename F>
constexpr auto transform (F &&handle_value) const &
 Calls a provided functor with the value channel if it is populated, and produces a new result with the return from it.
 
template<typename F>
constexpr auto transform (F &&handle_value) &&
 Calls a provided functor with the value channel if it is populated, and produces a new result with the return from it.
 
template<typename F>
constexpr auto transform (F &&handle_value) const &&
 Calls a provided functor with the value channel if it is populated, and produces a new result with the return from it.
 
template<typename F>
constexpr auto transform_error (F &&handle_error) &
 Calls a provided functor with the error channel if it is populated, and produces a new result with the return from it.
 
template<typename F>
constexpr auto transform_error (F &&handle_error) const &
 Calls a provided functor with the error channel if it is populated, and produces a new result with the return from it.
 
template<typename F>
constexpr auto transform_error (F &&handle_error) &&
 Calls a provided functor with the error channel if it is populated, and produces a new result with the return from it.
 
template<typename F>
constexpr auto transform_error (F &&handle_error) const &&
 Calls a provided functor with the error channel if it is populated, and produces a new result with the return from it.
 

Friends

ARENE_NODISCARD friend constexpr auto operator!= (result const &lhs, result const &rhs) noexcept(noexcept(lhs==rhs)) -> bool
 Compare two results for inequality.
 
ARENE_NODISCARD friend constexpr auto operator== (result const &lhs, result const &rhs) noexcept(noexcept(lhs.has_value(rhs.value())) &&noexcept(lhs.has_error(rhs.error()))) -> bool
 Compare two results for equality.
 
template<typename SourceValueType = T, typename SourceErrorType = E, constraints< std::enable_if_t< is_swappable_v< SourceValueType > >, std::enable_if_t< is_swappable_v< SourceErrorType > >, std::enable_if_t< std::is_nothrow_move_constructible< SourceValueType >::value >, std::enable_if_t< std::is_nothrow_move_constructible< SourceErrorType >::value > > = nullptr>
constexpr void swap (result &lhs, result &rhs) noexcept(noexcept(lhs.swap(rhs)))
 Swaps the state of two results.
 

Detailed Description

template<class T, class E>
class arene::base::result< T, E >

A sum type which can hold either a value or an error. Similar to std::expected.

Template Parameters
Tthe type of the value channel. May be void , otherwise must satisfy std::is_object and must not have const or volatile qualification.
Ethe type of the error channel. May not be void , must satisfy std::is_object and must not have const or volatile qualification.

result<T,E> can be used as a return type from an API to express the possibility that API being failable without the need for outparams or exceptions. Its design is non-allocating, and it its memory usage is similar to that of a union : it requires the amount of memory needed to store the larger of T or E , plus the space for the discriminator.

Semantically, result<T,E> contains two channels, the value channel and the error channel, and at any point during program execution exactly one of these channels is populated. A default constructed result<T,E> has its value_channel default constructed.

The following major operations are supported:

  • Querying whether the value or error channel is populated (optionally with a specific value/error).
  • Accessing the value/error channel's content. This is a checked operation, meaning that an ARENE_PRECONDITION violation will occur if the specified channel is not populated.
  • Equality Comparison if both T and E are equality comparable. results with different channels populated always compare unequal.
  • Assignment if both T and E are assignable.
  • swap if both T and E swappable and move-assignable.
  • std::hash if both T and E support std::hash .
  • Conversion to bool , which is equivalent to result<T,E>::has_value() .
  • Dereferencing the result , which is equivalent to accessing result::value() .

Some basic example usages:

// An API which either computes an @c int value, or produces @c error
arene::base::result<int, error> compute_foo(int);
// An API which either successfully writes all of @c bytes to some I/O sink, or returns an @c io_error .
arene::base::result<void, io_error> do_io(arene::base::span<arene::base::byte> bytes);

Please see the detailed [user manual](result) for usage examples and best practices.

Member Typedef Documentation

◆ error_type

template<class T, class E>
using arene::base::result< T, E >::error_type = E

The type when the result contains an error.

◆ value_type

template<class T, class E>
using arene::base::result< T, E >::value_type = T

The type when the result contains a value.

Constructor & Destructor Documentation

◆ result() [1/5]

template<class T, class E>
arene::base::result< T, E >::result ( )
constexprdefault

Constructs a result with a default-constructed value_type . Does not participate in overload resolution if value_type is not default constructible.

Postcondition
has_value() returns true
value() returns reference to an object equivalent to default-constructing value_type .

◆ result() [2/5]

template<class T, class E>
template<class... P, constraints< std::enable_if_t< std::is_constructible< value_type, P &&... >::value > > = nullptr>
arene::base::result< T, E >::result ( in_place_value_t ,
P &&... args )
inlineconstexprnoexcept

Constructs a result containing the value_type by way of direct-non-list-initialization.

Template Parameters
PThe type of the initializers. Must satisfy std::is_constructible<value_type,P&&...> .
Parameters
argsThe arguments to initialize the value channel with.
Postcondition
has_value() returns true
value() returns reference to an object equivalent to constructing value_type with args .

Example Usage:

class point {
int x_val;
int y_val;
public:
point(int x, int y)
: x_val(x),
y_val(y) {}
};
// position will have a value-channel populated equivalent to point{1,2}
arene::base::result<point, geometry_error> position(arene::base::in_place_value, 1, 2);

◆ result() [3/5]

template<class T, class E>
template<class... P, constraints< std::enable_if_t< std::is_constructible< error_type, P &&... >::value > > = nullptr>
arene::base::result< T, E >::result ( in_place_error_t ,
P &&... args )
inlineconstexprnoexcept

Constructs a result containing the error_type by way of direct-non-list-initialization.

Template Parameters
PThe type of the initializers. Must satisfy std::is_constructible<error_type,P&&...> .
Parameters
argsThe arguments to initialize the value channel with.
Postcondition
has_error() returns true
error() returns reference to an object equivalent to constructing error_type with args .

Example Usage:

class geometry_error {
int x_out_of_bounds_by;
int y_out_of_bounds_by;
public:
geometry_error(int x, int y)
: x_out_of_bounds_by(x),
y_out_of_bounds_by(y) {}
};
// position will have a error-channel populated equivalent to geometry_error{1,2}
arene::base::result<point, geometry_error> position(arene::base::in_place_error, 1, 2);

◆ result() [4/5]

template<class T, class E>
arene::base::result< T, E >::result ( result< T, E > const & copy) const &
constexprdefaultnoexcept

Default copy constructor.

◆ result() [5/5]

template<class T, class E>
arene::base::result< T, E >::result ( result< T, E > && move) &&
constexprdefaultnoexcept

Default move constructor.

◆ ~result()

template<class T, class E>
arene::base::result< T, E >::~result ( )
default

Destroy the stored value or error.

Member Function Documentation

◆ and_then() [1/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::and_then ( F && handle_value) &
inlineconstexpr

Monadic API which invokes a functor with the contents of value() if the value channel is populated.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of SelfType, and return result<U,error_type> , where U may be any valid value_type .
Parameters
handle_valuethe functor to invoke if self.has_value() is true .
Returns
result<U, error_type> which will be either the result of invoking handle_value with the contents of self.value() , or it will contain a copy of self.error() .

Basic usage example for opening a file:

auto maybe_file = arene::base::filesystem::directory_handle::open(dir_path).and_then(
[&filename](arene::base::filesystem::directory_handle dir) { return dir.open_file(filename); }
);

After this statement, maybe_file will contain either a valid instance of arene::base::file_handle in the value channel, or it will contain an instance of arene::base::error_code representing either the failure to open the file or the failure to open the directory.

◆ and_then() [2/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::and_then ( F && handle_value) &&
inlineconstexpr

Monadic API which invokes a functor with the contents of value() if the value channel is populated.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of SelfType, and return result<U,error_type> , where U may be any valid value_type .
Parameters
handle_valuethe functor to invoke if self.has_value() is true .
Returns
result<U, error_type> which will be either the result of invoking handle_value with the contents of self.value() , or it will contain a copy of self.error() .

Basic usage example for opening a file:

auto maybe_file = arene::base::filesystem::directory_handle::open(dir_path).and_then(
[&filename](arene::base::filesystem::directory_handle dir) { return dir.open_file(filename); }
);

After this statement, maybe_file will contain either a valid instance of arene::base::file_handle in the value channel, or it will contain an instance of arene::base::error_code representing either the failure to open the file or the failure to open the directory.

◆ and_then() [3/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::and_then ( F && handle_value) const &
inlineconstexpr

Monadic API which invokes a functor with the contents of value() if the value channel is populated.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of SelfType, and return result<U,error_type> , where U may be any valid value_type .
Parameters
handle_valuethe functor to invoke if self.has_value() is true .
Returns
result<U, error_type> which will be either the result of invoking handle_value with the contents of self.value() , or it will contain a copy of self.error() .

Basic usage example for opening a file:

auto maybe_file = arene::base::filesystem::directory_handle::open(dir_path).and_then(
[&filename](arene::base::filesystem::directory_handle dir) { return dir.open_file(filename); }
);

After this statement, maybe_file will contain either a valid instance of arene::base::file_handle in the value channel, or it will contain an instance of arene::base::error_code representing either the failure to open the file or the failure to open the directory.

◆ and_then() [4/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::and_then ( F && handle_value) const &&
inlineconstexpr

Monadic API which invokes a functor with the contents of value() if the value channel is populated.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of SelfType, and return result<U,error_type> , where U may be any valid value_type .
Parameters
handle_valuethe functor to invoke if self.has_value() is true .
Returns
result<U, error_type> which will be either the result of invoking handle_value with the contents of self.value() , or it will contain a copy of self.error() .

Basic usage example for opening a file:

auto maybe_file = arene::base::filesystem::directory_handle::open(dir_path).and_then(
[&filename](arene::base::filesystem::directory_handle dir) { return dir.open_file(filename); }
);

After this statement, maybe_file will contain either a valid instance of arene::base::file_handle in the value channel, or it will contain an instance of arene::base::error_code representing either the failure to open the file or the failure to open the directory.

◆ emplace()

template<class T, class E>
template<typename... ArgTypes, constraints< std::enable_if_t< std::is_nothrow_constructible< value_type, ArgTypes &&... >::value > > = nullptr>
auto arene::base::result< T, E >::emplace ( in_place_value_t ,
ArgTypes &&... args ) -> value_type&
inlinenoexcept

Emplaces a value into the result.

Template Parameters
ArgTypesthe types of the arguments to construct the value with
Parameters
argsThe arguments to construct the value with
Returns
A reference to the newly emplaced value
Postcondition
has_value() returns true
The content of value() will be equivalent to a value_type direct-initialized from args .

◆ error() [1/4]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::error ( ) && -> error_type&&
inlineconstexprnoexcept

Accesses a reference to the content of the error channel.

Precondition
has_error(), else ARENE_PRECONDITION violation
Returns
An rvalue reference to the contained error

◆ error() [2/4]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::error ( ) & -> error_type&
inlineconstexprnoexcept

Accesses a reference to the content of the error channel.

Precondition
has_error(), else ARENE_PRECONDITION violation
Returns
A reference to the contained error

◆ error() [3/4]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::error ( ) const && -> error_type const&&
inlineconstexprnoexcept

Accesses a reference to the content of the error channel.

Precondition
has_error(), else ARENE_PRECONDITION violation
Returns
A const rvalue reference to the contained error

◆ error() [4/4]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::error ( ) const & -> error_type const&
inlineconstexprnoexcept

Accesses a reference to the content of the error channel.

Precondition
has_error(), else ARENE_PRECONDITION violation
Returns
A const reference to the contained error

◆ has_value()

template<class T, class E>
template<typename U = value_type, constraints< std::enable_if_t< is_equality_comparable_v< U > > > = nullptr>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::has_value ( value_type const & val) const -> bool
inlineconstexprnoexcept

Checks if the value channel contains a specific value.

Parameters
valThe value to check for.
Returns
true If has_value() and value()==val .
false Otherwise.

◆ operator*() [1/4]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::operator* ( ) && -> value_type&&
inlineconstexprnoexcept

Accesses a reference to the content of the value channel.

Precondition
has_value(), else ARENE_PRECONDITION violation.
Returns
An rvalue reference to the contained value

◆ operator*() [2/4]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::operator* ( ) & -> value_type&
inlineconstexprnoexcept

Accesses a reference to the content of the value channel.

Precondition
has_value(), else ARENE_PRECONDITION violation.
Returns
A reference to the contained value

◆ operator*() [3/4]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::operator* ( ) const && -> value_type const&&
inlineconstexprnoexcept

Accesses a reference to the content of the value channel.

Precondition
has_value(), else ARENE_PRECONDITION violation.
Returns
A const rvalue reference to the contained value

◆ operator*() [4/4]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::operator* ( ) const & -> value_type const&
inlineconstexprnoexcept

Accesses a reference to the content of the value channel.

Precondition
has_value(), else ARENE_PRECONDITION violation.
Returns
A const reference to the contained value

◆ operator->() [1/2]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::operator-> ( ) const -> value_type const*
inlineconstexprnoexcept

Accesses a pointer to the content of the value channel.

Precondition
has_value(), else ARENE_PRECONDITION violation.
Returns
A pointer to the contained value

◆ operator->() [2/2]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::operator-> ( ) -> value_type*
inlineconstexprnoexcept

Accesses a pointer to the content of the value channel.

Precondition
has_value(), else ARENE_PRECONDITION violation.
Returns
A pointer to the contained value

◆ operator=() [1/4]

template<class T, class E>
auto arene::base::result< T, E >::operator= ( copy_assign_arg const & other) -> result&
inlineconstexprnoexcept

Copy-assignment for the case that value_type and error_type satisfy std::is_nothrow_copy_constructible and std::is_nothrow_copy_assignable, otherwise a dummy assignment operator that can never be called.

Parameters
otherThe value to copy from
Returns
*this

◆ operator=() [2/4]

template<class T, class E>
auto arene::base::result< T, E >::operator= ( deleted_copy_assign_arg const & other) -> result &=delete
constexprdeletenoexcept

Deleted Copy-assignment if value_type or error_type do not satisfy std::is_nothrow_copy_constructible and std::is_nothrow_copy_assignable, otherwise a deleted dummy assignment operator.

Parameters
otherThe value to copy from
Returns
*this

◆ operator=() [3/4]

template<class T, class E>
auto arene::base::result< T, E >::operator= ( deleted_move_assign_arg && other) -> result &=delete
constexprdeletenoexcept

Deleted Move-assignment if value_type or error_type do not satisfy std::is_nothrow_move_constructible and std::is_nothrow_move_assignable, otherwise a deleted dummy assignment operator.

Parameters
otherThe value to move from
Returns
*this

◆ operator=() [4/4]

template<class T, class E>
auto arene::base::result< T, E >::operator= ( move_assign_arg && other) -> result&
inlineconstexprnoexcept

Move-assignment for the case that value_type and error_type satisfy std::is_nothrow_move_constructible and std::is_nothrow_move_assignable, otherwise a dummy assignment operator that can never be called.

Parameters
otherThe value to move from
Returns
*this

◆ or_else() [1/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::or_else ( F && handle_error) &
inlineconstexpr

Monadic API which invokes a functor with the contents of error() if the error channel is populated.

Template Parameters
FType of the visiting functor. Must be invocable with error_type matching the const/ref qualification of SelfType and return result<value_type,E> , where E may be any valid error_type .
Parameters
handle_errorthe functor to invoke if self.has_error() is true .
Returns
result<value_type, E> which will be either the result of invoking handle_error with the contents of self.error() , or it will contain a copy of self.value() .

Basic usage example for opening a directory:

auto maybe_dir = arene::base::filesystem::directory_handle::open(dir_path).or_else(
[&logger](arene::base::filesystem::error_code err
) -> arene::base::result<arene::base::filesystem::directory_handle, arene::base::filesystem::error_code> {
logger.log(err);
}
);

◆ or_else() [2/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::or_else ( F && handle_error) &&
inlineconstexpr

Monadic API which invokes a functor with the contents of error() if the error channel is populated.

Template Parameters
FType of the visiting functor. Must be invocable with error_type matching the const/ref qualification of SelfType and return result<value_type,E> , where E may be any valid error_type .
Parameters
handle_errorthe functor to invoke if self.has_error() is true .
Returns
result<value_type, E> which will be either the result of invoking handle_error with the contents of self.error() , or it will contain a copy of self.value() .

Basic usage example for opening a directory:

auto maybe_dir = arene::base::filesystem::directory_handle::open(dir_path).or_else(
[&logger](arene::base::filesystem::error_code err
) -> arene::base::result<arene::base::filesystem::directory_handle, arene::base::filesystem::error_code> {
logger.log(err);
}
);

◆ or_else() [3/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::or_else ( F && handle_error) const &
inlineconstexpr

Monadic API which invokes a functor with the contents of error() if the error channel is populated.

Template Parameters
FType of the visiting functor. Must be invocable with error_type matching the const/ref qualification of SelfType and return result<value_type,E> , where E may be any valid error_type .
Parameters
handle_errorthe functor to invoke if self.has_error() is true .
Returns
result<value_type, E> which will be either the result of invoking handle_error with the contents of self.error() , or it will contain a copy of self.value() .

Basic usage example for opening a directory:

auto maybe_dir = arene::base::filesystem::directory_handle::open(dir_path).or_else(
[&logger](arene::base::filesystem::error_code err
) -> arene::base::result<arene::base::filesystem::directory_handle, arene::base::filesystem::error_code> {
logger.log(err);
}
);

◆ or_else() [4/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::or_else ( F && handle_error) const &&
inlineconstexpr

Monadic API which invokes a functor with the contents of error() if the error channel is populated.

Template Parameters
FType of the visiting functor. Must be invocable with error_type matching the const/ref qualification of SelfType and return result<value_type,E> , where E may be any valid error_type .
Parameters
handle_errorthe functor to invoke if self.has_error() is true .
Returns
result<value_type, E> which will be either the result of invoking handle_error with the contents of self.error() , or it will contain a copy of self.value() .

Basic usage example for opening a directory:

auto maybe_dir = arene::base::filesystem::directory_handle::open(dir_path).or_else(
[&logger](arene::base::filesystem::error_code err
) -> arene::base::result<arene::base::filesystem::directory_handle, arene::base::filesystem::error_code> {
logger.log(err);
}
);

◆ swap()

template<class T, class E>
template<typename SourceValueType = T, typename SourceErrorType = E, constraints< std::enable_if_t< is_swappable_v< SourceValueType > >, std::enable_if_t< is_swappable_v< SourceErrorType > >, std::enable_if_t< std::is_nothrow_move_constructible< SourceValueType >::value >, std::enable_if_t< std::is_nothrow_move_constructible< SourceErrorType >::value > > = nullptr>
void arene::base::result< T, E >::swap ( result< T, E > & other)
inlineconstexprnoexcept

Swaps the state of two results.

Template Parameters
SourceValueTypeMust satisfy is_swappable_v<SourceValueType> and std::is_nothrow_move_constructible<SourceValueType>
SourceErrorTypeMust satisfy is_swappable_v<SourceErrorType> and std::is_nothrow_move_constructible<SourceErrorType>
Parameters
otherThe result to swap with
Postcondition
If both this and other have matching channels populated, the content of those channels are swapped as if via arene::base::swap . Otherwise, it is equivalent to emplacing this with the active channel from other , and vice-versa.

◆ transform() [1/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::transform ( F && handle_value) &
inlineconstexpr

Calls a provided functor with the value channel if it is populated, and produces a new result with the return from it.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of this . May return any valid value_type
Parameters
handle_valuethe functor to invoke if has_value() is true .
Returns
result<U, error_type> which will be one of:
  • A result<U,error_type> constructed from the result of invoking handle_value with the contents of value() if value_type is not void and the return from F is not void
  • A result<void,error_type> constructed from the result of invoking handle_value with the contents of value() if value_type is not void and the return from F is void
  • A result<U,error_type> where U is the return type of F constructed from a copy of error() .

◆ transform() [2/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::transform ( F && handle_value) &&
inlineconstexpr

Calls a provided functor with the value channel if it is populated, and produces a new result with the return from it.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of this . May return any valid value_type
Parameters
handle_valuethe functor to invoke if has_value() is true .
Returns
result<U, error_type> which will be one of:
  • A result<U,error_type> constructed from the result of invoking handle_value with the contents of value() if value_type is not void and the return from F is not void
  • A result<void,error_type> constructed from the result of invoking handle_value with the contents of value() if value_type is not void and the return from F is void
  • A result<U,error_type> where U is the return type of F constructed from a copy of error() .

◆ transform() [3/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::transform ( F && handle_value) const &
inlineconstexpr

Calls a provided functor with the value channel if it is populated, and produces a new result with the return from it.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of this . May return any valid value_type
Parameters
handle_valuethe functor to invoke if has_value() is true .
Returns
result<U, error_type> which will be one of:
  • A result<U,error_type> constructed from the result of invoking handle_value with the contents of value() if value_type is not void and the return from F is not void
  • A result<void,error_type> constructed from the result of invoking handle_value with the contents of value() if value_type is not void and the return from F is void
  • A result<U,error_type> where U is the return type of F constructed from a copy of error() .

◆ transform() [4/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::transform ( F && handle_value) const &&
inlineconstexpr

Calls a provided functor with the value channel if it is populated, and produces a new result with the return from it.

Template Parameters
FType of the visiting functor. Must be invocable with value_type matching the const/ref qualification of this . May return any valid value_type
Parameters
handle_valuethe functor to invoke if has_value() is true .
Returns
result<U, error_type> which will be one of:
  • A result<U,error_type> constructed from the result of invoking handle_value with the contents of value() if value_type is not void and the return from F is not void
  • A result<void,error_type> constructed from the result of invoking handle_value with the contents of value() if value_type is not void and the return from F is void
  • A result<U,error_type> where U is the return type of F constructed from a copy of error() .

◆ transform_error() [1/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::transform_error ( F && handle_error) &
inlineconstexpr

Calls a provided functor with the error channel if it is populated, and produces a new result with the return from it.

Template Parameters
FType of the visiting functor. Must be invocable with error_type matching the const/ref qualification of this. May return any valid error_type .
Parameters
handle_errorthe functor to invoke if self.has_error() is true .
Returns
result<value_type, E> which will be either:
  • A result constructed from the result of invoking handle_error with the contents of self.error() ,
  • A result constructed from a copy of self.value() .

◆ transform_error() [2/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::transform_error ( F && handle_error) &&
inlineconstexpr

Calls a provided functor with the error channel if it is populated, and produces a new result with the return from it.

Template Parameters
FType of the visiting functor. Must be invocable with error_type matching the const/ref qualification of this. May return any valid error_type .
Parameters
handle_errorthe functor to invoke if self.has_error() is true .
Returns
result<value_type, E> which will be either:
  • A result constructed from the result of invoking handle_error with the contents of self.error() ,
  • A result constructed from a copy of self.value() .

◆ transform_error() [3/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::transform_error ( F && handle_error) const &
inlineconstexpr

Calls a provided functor with the error channel if it is populated, and produces a new result with the return from it.

Template Parameters
FType of the visiting functor. Must be invocable with error_type matching the const/ref qualification of this. May return any valid error_type .
Parameters
handle_errorthe functor to invoke if self.has_error() is true .
Returns
result<value_type, E> which will be either:
  • A result constructed from the result of invoking handle_error with the contents of self.error() ,
  • A result constructed from a copy of self.value() .

◆ transform_error() [4/4]

template<class T, class E>
template<typename F>
auto arene::base::result< T, E >::transform_error ( F && handle_error) const &&
inlineconstexpr

Calls a provided functor with the error channel if it is populated, and produces a new result with the return from it.

Template Parameters
FType of the visiting functor. Must be invocable with error_type matching the const/ref qualification of this. May return any valid error_type .
Parameters
handle_errorthe functor to invoke if self.has_error() is true .
Returns
result<value_type, E> which will be either:
  • A result constructed from the result of invoking handle_error with the contents of self.error() ,
  • A result constructed from a copy of self.value() .

◆ value() [1/4]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::value ( ) && -> value_type&&
inlineconstexprnoexcept

Accesses a reference to the content of the value channel.

Precondition
has_value(), else ARENE_PRECONDITION violation.
Returns
An rvalue reference to the contained value

◆ value() [2/4]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::value ( ) & -> value_type&
inlineconstexprnoexcept

Accesses a reference to the content of the value channel.

Precondition
has_value(), else ARENE_PRECONDITION violation.
Returns
A reference to the contained value

◆ value() [3/4]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::value ( ) const && -> value_type const&&
inlineconstexprnoexcept

Accesses a reference to the content of the value channel.

Precondition
has_value(), else ARENE_PRECONDITION violation.
Returns
A const rvalue reference to the contained value

◆ value() [4/4]

template<class T, class E>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::value ( ) const & -> value_type const&
inlineconstexprnoexcept

Accesses a reference to the content of the value channel.

Precondition
has_value(), else ARENE_PRECONDITION violation.
Returns
A const reference to the contained value

◆ value_or() [1/2]

template<class T, class E>
template<typename U = T, constraints< std::enable_if_t< std::is_convertible< U, value_type >::value > > = nullptr>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::value_or ( U && default_value) && -> value_type
inlineconstexprnoexcept

Returns the value if the value channel is populated, else another value. Not declared if T is void .

Template Parameters
UThe type of the default value. Must satisfy std::is_convertible<U,value_type> .
Parameters
default_valueThe default value to use if the value channel is not populated.
Returns
value_type If has_value() is true, equivalent to value() . Otherwise, default_value converted to value_type .

◆ value_or() [2/2]

template<class T, class E>
template<typename U = T, constraints< std::enable_if_t< std::is_convertible< U, value_type >::value > > = nullptr>
ARENE_NODISCARD constexpr auto arene::base::result< T, E >::value_or ( U && default_value) const & -> value_type
inlineconstexprnoexcept

Returns the value if the value channel is populated, else another value. Not declared if T is void .

Template Parameters
UThe type of the default value. Must satisfy std::is_convertible<U,value_type> .
Parameters
default_valueThe default value to use if the value channel is not populated.
Returns
value_type If has_value() is true, equivalent to value() . Otherwise, default_value converted to value_type .

Friends And Related Symbol Documentation

◆ operator!=

template<class T, class E>
ARENE_NODISCARD friend constexpr auto operator!= ( result< T, E > const & lhs,
result< T, E > const & rhs ) -> bool
friend

Compare two results for inequality.

Parameters
lhsThe first result
rhsThe second result
Returns
bool true if the both lhs and rhs hold values that are not equal, or both hold errors that are not equal, or one holds a value and the other holds an error, false otherwise

◆ operator==

template<class T, class E>
ARENE_NODISCARD friend constexpr auto operator== ( result< T, E > const & lhs,
result< T, E > const & rhs ) -> bool
friend

Compare two results for equality.

Parameters
lhsThe first result
rhsThe second result
Returns
bool true if the both lhs and rhs hold values that are equal, or both hold errors that are equal, false otherwise

◆ swap

template<class T, class E>
template<typename SourceValueType = T, typename SourceErrorType = E, constraints< std::enable_if_t< is_swappable_v< SourceValueType > >, std::enable_if_t< is_swappable_v< SourceErrorType > >, std::enable_if_t< std::is_nothrow_move_constructible< SourceValueType >::value >, std::enable_if_t< std::is_nothrow_move_constructible< SourceErrorType >::value > > = nullptr>
void swap ( result< T, E > & lhs,
result< T, E > & rhs )
friend

Swaps the state of two results.

Template Parameters
SourceValueTypeMust satisfy is_swappable_v<SourceValueType> , std::is_nothrow_move_constructible<SourceValueType>
SourceErrorTypeMust satisfy is_swappable_v<SourceErrorType> , std::is_nothrow_move_constructible<SourceErrorType>
Parameters
lhsThe left hand result to swap
rhsThe right hand result to swap
Postcondition
If both lhs and rhs have matching channels populated, the content of those channels are swapped as if via arene::base::swap . Otherwise, it is equivalent to emplacing lhs with the active channel from rhs , and vise-versa.

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