![]() |
Arene Base
Fundamental Utilities For Safety Critical C++
|
A sum type which can hold either a value or an error. Similar to std::expected. More...

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 ©) 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. | |
A sum type which can hold either a value or an error. Similar to std::expected.
| T | the type of the value channel. May be void , otherwise must satisfy std::is_object and must not have const or volatile qualification. |
| E | the 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:
ARENE_PRECONDITION violation will occur if the specified channel is not populated.T and E are equality comparable. results with different channels populated always compare unequal.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 .bool , which is equivalent to result<T,E>::has_value() .result , which is equivalent to accessing result::value() .Some basic example usages:
Please see the detailed [user manual](result) for usage examples and best practices.
| using arene::base::result< T, E >::error_type = E |
The type when the result contains an error.
| using arene::base::result< T, E >::value_type = T |
The type when the result contains a value.
|
constexprdefault |
Constructs a result with a default-constructed value_type . Does not participate in overload resolution if value_type is not default constructible.
has_value() returns true value() returns reference to an object equivalent to default-constructing value_type .
|
inlineconstexprnoexcept |
Constructs a result containing the value_type by way of direct-non-list-initialization.
| P | The type of the initializers. Must satisfy std::is_constructible<value_type,P&&...> . |
| args | The arguments to initialize the value channel with. |
has_value() returns true value() returns reference to an object equivalent to constructing value_type with args .Example Usage:
|
inlineconstexprnoexcept |
Constructs a result containing the error_type by way of direct-non-list-initialization.
| P | The type of the initializers. Must satisfy std::is_constructible<error_type,P&&...> . |
| args | The arguments to initialize the value channel with. |
has_error() returns true error() returns reference to an object equivalent to constructing error_type with args .Example Usage:
|
constexprdefaultnoexcept |
Default copy constructor.
|
constexprdefaultnoexcept |
Default move constructor.
|
default |
Destroy the stored value or error.
|
inlineconstexpr |
Monadic API which invokes a functor with the contents of value() if the value channel is populated.
| F | Type 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 . |
| handle_value | the functor to invoke if self.has_value() is true . |
handle_value with the contents of self.value() , or it will contain a copy of self.error() .Basic usage example for opening a file:
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.
|
inlineconstexpr |
Monadic API which invokes a functor with the contents of value() if the value channel is populated.
| F | Type 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 . |
| handle_value | the functor to invoke if self.has_value() is true . |
handle_value with the contents of self.value() , or it will contain a copy of self.error() .Basic usage example for opening a file:
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.
|
inlineconstexpr |
Monadic API which invokes a functor with the contents of value() if the value channel is populated.
| F | Type 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 . |
| handle_value | the functor to invoke if self.has_value() is true . |
handle_value with the contents of self.value() , or it will contain a copy of self.error() .Basic usage example for opening a file:
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.
|
inlineconstexpr |
Monadic API which invokes a functor with the contents of value() if the value channel is populated.
| F | Type 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 . |
| handle_value | the functor to invoke if self.has_value() is true . |
handle_value with the contents of self.value() , or it will contain a copy of self.error() .Basic usage example for opening a file:
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.
|
inlinenoexcept |
Emplaces a value into the result.
| ArgTypes | the types of the arguments to construct the value with |
| args | The arguments to construct the value with |
has_value() returns true value() will be equivalent to a value_type direct-initialized from args .
|
inlineconstexprnoexcept |
Accesses a reference to the content of the error channel.
|
inlineconstexprnoexcept |
Accesses a reference to the content of the error channel.
|
inlineconstexprnoexcept |
Accesses a reference to the content of the error channel.
|
inlineconstexprnoexcept |
Accesses a reference to the content of the error channel.
|
inlineconstexprnoexcept |
Checks if the value channel contains a specific value.
| val | The value to check for. |
has_value() and value()==val .
|
inlineconstexprnoexcept |
Accesses a reference to the content of the value channel.
|
inlineconstexprnoexcept |
Accesses a reference to the content of the value channel.
|
inlineconstexprnoexcept |
Accesses a reference to the content of the value channel.
|
inlineconstexprnoexcept |
Accesses a reference to the content of the value channel.
|
inlineconstexprnoexcept |
Accesses a pointer to the content of the value channel.
|
inlineconstexprnoexcept |
Accesses a pointer to the content of the value channel.
|
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.
| other | The value to copy from |
|
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.
| other | The value to copy from |
|
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.
| other | The value to move from |
|
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.
| other | The value to move from |
|
inlineconstexpr |
Monadic API which invokes a functor with the contents of error() if the error channel is populated.
| F | Type 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 . |
| handle_error | the functor to invoke if self.has_error() is true . |
handle_error with the contents of self.error() , or it will contain a copy of self.value() .Basic usage example for opening a directory:
|
inlineconstexpr |
Monadic API which invokes a functor with the contents of error() if the error channel is populated.
| F | Type 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 . |
| handle_error | the functor to invoke if self.has_error() is true . |
handle_error with the contents of self.error() , or it will contain a copy of self.value() .Basic usage example for opening a directory:
|
inlineconstexpr |
Monadic API which invokes a functor with the contents of error() if the error channel is populated.
| F | Type 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 . |
| handle_error | the functor to invoke if self.has_error() is true . |
handle_error with the contents of self.error() , or it will contain a copy of self.value() .Basic usage example for opening a directory:
|
inlineconstexpr |
Monadic API which invokes a functor with the contents of error() if the error channel is populated.
| F | Type 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 . |
| handle_error | the functor to invoke if self.has_error() is true . |
handle_error with the contents of self.error() , or it will contain a copy of self.value() .Basic usage example for opening a directory:
|
inlineconstexprnoexcept |
Swaps the state of two results.
| SourceValueType | Must satisfy is_swappable_v<SourceValueType> and std::is_nothrow_move_constructible<SourceValueType> |
| SourceErrorType | Must satisfy is_swappable_v<SourceErrorType> and std::is_nothrow_move_constructible<SourceErrorType> |
| other | The result to swap with |
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.
|
inlineconstexpr |
Calls a provided functor with the value channel if it is populated, and produces a new result with the return from it.
| F | Type of the visiting functor. Must be invocable with value_type matching the const/ref qualification of this . May return any valid value_type |
| handle_value | the functor to invoke if has_value() is true . |
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 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 result<U,error_type> where U is the return type of F constructed from a copy of error() .
|
inlineconstexpr |
Calls a provided functor with the value channel if it is populated, and produces a new result with the return from it.
| F | Type of the visiting functor. Must be invocable with value_type matching the const/ref qualification of this . May return any valid value_type |
| handle_value | the functor to invoke if has_value() is true . |
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 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 result<U,error_type> where U is the return type of F constructed from a copy of error() .
|
inlineconstexpr |
Calls a provided functor with the value channel if it is populated, and produces a new result with the return from it.
| F | Type of the visiting functor. Must be invocable with value_type matching the const/ref qualification of this . May return any valid value_type |
| handle_value | the functor to invoke if has_value() is true . |
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 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 result<U,error_type> where U is the return type of F constructed from a copy of error() .
|
inlineconstexpr |
Calls a provided functor with the value channel if it is populated, and produces a new result with the return from it.
| F | Type of the visiting functor. Must be invocable with value_type matching the const/ref qualification of this . May return any valid value_type |
| handle_value | the functor to invoke if has_value() is true . |
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 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 result<U,error_type> where U is the return type of F constructed from a copy of error() .
|
inlineconstexpr |
Calls a provided functor with the error channel if it is populated, and produces a new result with the return from it.
| F | Type of the visiting functor. Must be invocable with error_type matching the const/ref qualification of this. May return any valid error_type . |
| handle_error | the functor to invoke if self.has_error() is true . |
handle_error with the contents of self.error() ,self.value() .
|
inlineconstexpr |
Calls a provided functor with the error channel if it is populated, and produces a new result with the return from it.
| F | Type of the visiting functor. Must be invocable with error_type matching the const/ref qualification of this. May return any valid error_type . |
| handle_error | the functor to invoke if self.has_error() is true . |
handle_error with the contents of self.error() ,self.value() .
|
inlineconstexpr |
Calls a provided functor with the error channel if it is populated, and produces a new result with the return from it.
| F | Type of the visiting functor. Must be invocable with error_type matching the const/ref qualification of this. May return any valid error_type . |
| handle_error | the functor to invoke if self.has_error() is true . |
handle_error with the contents of self.error() ,self.value() .
|
inlineconstexpr |
Calls a provided functor with the error channel if it is populated, and produces a new result with the return from it.
| F | Type of the visiting functor. Must be invocable with error_type matching the const/ref qualification of this. May return any valid error_type . |
| handle_error | the functor to invoke if self.has_error() is true . |
handle_error with the contents of self.error() ,self.value() .
|
inlineconstexprnoexcept |
Accesses a reference to the content of the value channel.
|
inlineconstexprnoexcept |
Accesses a reference to the content of the value channel.
|
inlineconstexprnoexcept |
Accesses a reference to the content of the value channel.
|
inlineconstexprnoexcept |
Accesses a reference to the content of the value channel.
|
inlineconstexprnoexcept |
Returns the value if the value channel is populated, else another value. Not declared if T is void .
| U | The type of the default value. Must satisfy std::is_convertible<U,value_type> . |
| default_value | The default value to use if the value channel is not populated. |
has_value() is true, equivalent to value() . Otherwise, default_value converted to value_type .
|
inlineconstexprnoexcept |
Returns the value if the value channel is populated, else another value. Not declared if T is void .
| U | The type of the default value. Must satisfy std::is_convertible<U,value_type> . |
| default_value | The default value to use if the value channel is not populated. |
has_value() is true, equivalent to value() . Otherwise, default_value converted to value_type .
|
friend |
Compare two results for inequality.
| lhs | The first result |
| rhs | The second result |
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
|
friend |
Compare two results for equality.
| lhs | The first result |
| rhs | The second result |
true if the both lhs and rhs hold values that are equal, or both hold errors that are equal, false otherwise
|
friend |
Swaps the state of two results.
| SourceValueType | Must satisfy is_swappable_v<SourceValueType> , std::is_nothrow_move_constructible<SourceValueType> |
| SourceErrorType | Must satisfy is_swappable_v<SourceErrorType> , std::is_nothrow_move_constructible<SourceErrorType> |
| lhs | The left hand result to swap |
| rhs | The right hand result to swap |
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.