cf::expected¶
A wrapper that contains either a value or an error. More...
#include <expected.hpp>
Public Types¶
| Name | |
|---|---|
| using T | value_type Value type alias. |
| using E | error_type Error type alias. |
| using unexpected< E > | unexpected_type Unexpected type alias. |
Public Functions¶
| Name | |
|---|---|
| constexpr | expected() Default constructor. Value-initializes T. |
| expected(constexpected & o) Copy constructor. |
|
| expected(expected && o) Move constructor. |
|
| template <typenameU =T,typename =std::enable_if_t<!std::is_same_v constexpr |
expected(U && v) Converting constructor from value. |
| template <typenameG =E,typename =std::enable_if_t constexpr |
expected(constunexpected< G > & u) Constructor from unexpected. |
| template <typenameG =E,typename =std::enable_if_t constexpr |
expected(unexpected< G > && u) Constructor from unexpected rvalue. |
| template <typename... Args,typename =std::enable_if_t constexpr |
expected(std::in_place_t , Args &&... args) In-place constructor for value. |
| template <typename... Args,typename =std::enable_if_t constexpr |
expected(unexpect_t , Args &&... args) Constructor for in-place error construction. |
| ~expected() Destructor. Destroys contained value or error. |
|
| expected & | operator=(constexpected & o) Copy assignment operator. |
| expected & | operator=(expected && o) Move assignment operator. |
| template <typenameU > std::enable_if_t<!std::is_same_v< std::decay_t< U >, expected > &&std::is_constructible_v< T, U > &&std::is_assignable_v< T &, U >, expected & > |
operator=(U && v) Assignment from value. |
| template <typenameG > expected & |
operator=(constunexpected< G > & u) Assignment from unexpected. |
| template <typenameG > expected & |
operator=(unexpected< G > && u) Assignment from unexpected rvalue. |
| constexprbool | has_value() const Checks if the expected contains a value. |
| constexpr | operator bool() const Boolean conversion operator. |
| constexprT * | operator->() Pointer operator. |
| constexprconstT * | operator->() const Const pointer operator. |
| constexprT & | operator*() Dereference operator. |
| constexprconstT & | operator*() const Const dereference operator. |
| constexprT && | operator*() Rvalue dereference operator. |
| constexprconstT && | operator*() const Const rvalue dereference operator. |
| constexprT & | value() Returns the contained value. |
| constexprconstT & | value() const Returns the contained value (const lvalue). |
| constexprT && | value() Returns the contained value (rvalue). |
| constexprconstT && | value() const Returns the contained value (const rvalue). |
| constexprE & | error() Returns the contained error. |
| constexprconstE & | error() const Returns the contained error (const lvalue). |
| constexprE && | error() Returns the contained error (rvalue). |
| constexprconstE && | error() const Returns the contained error (const rvalue). |
| template <typenameU > constexprT |
value_or(U && default_val) const Returns the contained value or a default value. |
| template <typenameU > constexprT |
value_or(U && default_val) Returns the contained value or a default value (rvalue). |
| template <typenameU > constexprE |
error_or(U && default_err) const Returns the contained error or a default error. |
| template <typenameU > constexprE |
error_or(U && default_err) Returns the contained error or a default error (rvalue). |
| template <typenameF > auto |
and_then(F && f) Monadic operation: chains if value is present. |
| template <typenameF > auto |
and_then(F && f) const Monadic operation: chains if value is present (const). |
| template <typenameF > auto |
and_then(F && f) Monadic operation: chains if value is present (rvalue). |
| template <typenameF > auto |
or_else(F && f) Monadic operation: chains if error is present. |
| template <typenameF > auto |
or_else(F && f) const Monadic operation: chains if error is present (const). |
| template <typenameF > auto |
or_else(F && f) Monadic operation: chains if error is present (rvalue). |
| template <typenameF > auto |
transform(F && f) Monadic operation: transforms the value. |
| template <typenameF > auto |
transform(F && f) const Monadic operation: transforms the value (const). |
| template <typenameF > auto |
transform(F && f) Monadic operation: transforms the value (rvalue). |
| template <typenameF > auto |
transform_error(F && f) Monadic operation: transforms the error. |
| template <typenameF > auto |
transform_error(F && f) const Monadic operation: transforms the error (const). |
| template <typenameF > auto |
transform_error(F && f) Monadic operation: transforms the error (rvalue). |
| void | swap(expected & o) Swaps the contents with another expected. |
Friends¶
| Name | |
|---|---|
| constexprbool | operator==(constexpected & a, constexpected< T2, E2 > & b) Equality comparison with another expected. |
| constexprbool | operator==(constexpected & a, constT2 & v) Equality comparison with a value. |
| constexprbool | operator==(constexpected & a, constunexpected< E2 > & u) Equality comparison with unexpected. |
| constexprbool | operator!=(constexpected & a, constexpected< T2, E2 > & b) Inequality comparison with another expected. |
| constexprbool | operator!=(constexpected & a, constT2 & v) Inequality comparison with a value. |
| constexprbool | operator!=(constexpected & a, constunexpected< E2 > & u) Inequality comparison with unexpected. |
| void | swap(expected & a, expected & b) Non-member swap function. |
Detailed Description¶
A wrapper that contains either a value or an error.
Template Parameters:
- T Value type.
- E Error type.
Provides type-safe error handling without exceptions. The object either contains a value of type T or an error of type E.
Public Types Documentation¶
using value_type¶
Value type alias.
using error_type¶
Error type alias.
using unexpected_type¶
Unexpected type alias.
Public Functions Documentation¶
function expected¶
Default constructor. Value-initializes T.
Exceptions:
- May throw if T default constructor throws.
function expected¶
Copy constructor.
Parameters:
- o Expected to copy from.
Exceptions:
- May throw if T or E copy constructor throws.
function expected¶
Move constructor.
Parameters:
- o Expected to move from.
Exceptions:
- May throw if T or E move constructor throws.
function expected¶
template <typenameU =T,
typename =std::enable_if_t<!std::is_same_v<std::decay_t<U>, expected> && !std::is_same_v<std::decay_t<U>, std::in_place_t> && !std::is_same_v<std::decay_t<U>, unexpect_t> && std::is_constructible_v<T, U>>>
inline constexpr expected(
U && v
)
Converting constructor from value.
Parameters:
- v Value to construct with.
Exceptions:
- May throw if T constructor throws.
Template Parameters:
- U Forwarded value type.
function expected¶
template <typenameG =E,
typename =std::enable_if_t<std::is_constructible_v<E, constG&>>>
inline constexpr expected(
constunexpected< G > & u
)
Constructor from unexpected.
Parameters:
- u Unexpected value containing error.
Exceptions:
- May throw if E constructor throws.
Template Parameters:
- G Forwarded error type.
function expected¶
template <typenameG =E,
typename =std::enable_if_t<std::is_constructible_v<E, G>>>
inline constexpr expected(
unexpected< G > && u
)
Constructor from unexpected rvalue.
Parameters:
- u Unexpected value containing error.
Exceptions:
- May throw if E constructor throws.
Template Parameters:
- G Forwarded error type.
function expected¶
template <typename... Args,
typename =std::enable_if_t<std::is_constructible_v<T, Args...>>>
inline explicit constexpr expected(
std::in_place_t ,
Args &&... args
)
In-place constructor for value.
Parameters:
- args Arguments to forward to T constructor.
Exceptions:
- May throw if T constructor throws.
Template Parameters:
- Args Constructor argument types.
function expected¶
template <typename... Args,
typename =std::enable_if_t<std::is_constructible_v<E, Args...>>>
inline explicit constexpr expected(
unexpect_t ,
Args &&... args
)
Constructor for in-place error construction.
Parameters:
- args Arguments to forward to E constructor.
Exceptions:
- May throw if E constructor throws.
Template Parameters:
- Args Constructor argument types.
function ~expected¶
Destructor. Destroys contained value or error.
Exceptions:
- None.
function operator=¶
Copy assignment operator.
Parameters:
- o Expected to copy from.
Exceptions:
- May throw if assignment or construction throws.
Return: Reference to this expected.
function operator=¶
Move assignment operator.
Parameters:
- o Expected to move from.
Exceptions:
- May throw if assignment or construction throws.
Return: Reference to this expected.
function operator=¶
template <typenameU >
inline std::enable_if_t<!std::is_same_v< std::decay_t< U >, expected > &&std::is_constructible_v< T, U > &&std::is_assignable_v< T &, U >, expected & > operator=(
U && v
)
Assignment from value.
Parameters:
- v Value to assign.
Exceptions:
- May throw if assignment or construction throws.
Template Parameters:
- U Forwarded value type.
Return: Reference to this expected.
function operator=¶
Assignment from unexpected.
Parameters:
- u Unexpected value containing error.
Exceptions:
- May throw if assignment or construction throws.
Template Parameters:
- G Forwarded error type.
Return: Reference to this expected.
function operator=¶
Assignment from unexpected rvalue.
Parameters:
- u Unexpected value containing error.
Exceptions:
- May throw if assignment or construction throws.
Template Parameters:
- G Forwarded error type.
Return: Reference to this expected.
function has_value¶
Checks if the expected contains a value.
Exceptions:
- None.
Return: true if contains a value, false if contains error.
function operator bool¶
Boolean conversion operator.
Exceptions:
- None.
Return: true if contains a value, false if contains error.
function operator->¶
Pointer operator.
Exceptions:
- None.
Return: Pointer to the contained value.
Warning: Undefined behavior if expected does not contain a value.
function operator->¶
Const pointer operator.
Exceptions:
- None.
Return: Pointer to the contained value.
Warning: Undefined behavior if expected does not contain a value.
function operator*¶
Dereference operator.
Exceptions:
- None.
Return: Reference to the contained value.
Warning: Undefined behavior if expected does not contain a value.
function operator*¶
Const dereference operator.
Exceptions:
- None.
Return: Const reference to the contained value.
Warning: Undefined behavior if expected does not contain a value.
function operator*¶
Rvalue dereference operator.
Exceptions:
- None.
Return: Rvalue reference to the contained value.
Warning: Undefined behavior if expected does not contain a value.
function operator*¶
Const rvalue dereference operator.
Exceptions:
- None.
Return: Const rvalue reference to the contained value.
Warning: Undefined behavior if expected does not contain a value.
function value¶
Returns the contained value.
Exceptions:
- bad_expected_access if expected contains an error.
Return: Reference to the contained value.
Warning: Throws bad_expected_access if this expected does not contain a value.
function value¶
Returns the contained value (const lvalue).
Exceptions:
- bad_expected_access if expected contains an error.
Return: Const reference to the contained value.
Warning: Throws bad_expected_access if this expected does not contain a value.
function value¶
Returns the contained value (rvalue).
Exceptions:
- bad_expected_access if expected contains an error.
Return: Rvalue reference to the contained value.
Warning: Throws bad_expected_access if this expected does not contain a value.
function value¶
Returns the contained value (const rvalue).
Exceptions:
- bad_expected_access if expected contains an error.
Return: Const rvalue reference to the contained value.
Warning: Throws bad_expected_access if this expected does not contain a value.
function error¶
Returns the contained error.
Exceptions:
- None.
Return: Reference to the contained error.
Warning: The error is only valid if has_value() returns false.
function error¶
Returns the contained error (const lvalue).
Exceptions:
- None.
Return: Const reference to the contained error.
Warning: The error is only valid if has_value() returns false.
function error¶
Returns the contained error (rvalue).
Exceptions:
- None.
Return: Rvalue reference to the contained error.
Warning: The error is only valid if has_value() returns false.
function error¶
Returns the contained error (const rvalue).
Exceptions:
- None.
Return: Const rvalue reference to the contained error.
Warning: The error is only valid if has_value() returns false.
function value_or¶
Returns the contained value or a default value.
Parameters:
- default_val Default value to return if in error state.
Exceptions:
- None.
Template Parameters:
- U Default value type.
Return: The contained value or the default value.
function value_or¶
Returns the contained value or a default value (rvalue).
Parameters:
- default_val Default value to return if in error state.
Exceptions:
- None.
Template Parameters:
- U Default value type.
Return: The contained value or the default value.
function error_or¶
Returns the contained error or a default error.
Parameters:
- default_err Default error to return if has value.
Exceptions:
- None.
Template Parameters:
- U Default error type.
Return: The contained error or the default error.
function error_or¶
Returns the contained error or a default error (rvalue).
Parameters:
- default_err Default error to return if has value.
Exceptions:
- None.
Template Parameters:
- U Default error type.
Return: The contained error or the default error.
function and_then¶
Monadic operation: chains if value is present.
Parameters:
- f Function to call if value is present.
Exceptions:
- None.
Template Parameters:
- F Callable type.
Return: Result of f(value) or error.
function and_then¶
Monadic operation: chains if value is present (const).
Parameters:
- f Function to call if value is present.
Exceptions:
- None.
Template Parameters:
- F Callable type.
Return: Result of f(value) or error.
function and_then¶
Monadic operation: chains if value is present (rvalue).
Parameters:
- f Function to call if value is present.
Exceptions:
- None.
Template Parameters:
- F Callable type.
Return: Result of f(value) or error.
function or_else¶
Monadic operation: chains if error is present.
Parameters:
- f Function to call if error is present.
Exceptions:
- None.
Template Parameters:
- F Callable type.
Return: value or result of f(error).
function or_else¶
Monadic operation: chains if error is present (const).
Parameters:
- f Function to call if error is present.
Exceptions:
- None.
Template Parameters:
- F Callable type.
Return: value or result of f(error).
function or_else¶
Monadic operation: chains if error is present (rvalue).
Parameters:
- f Function to call if error is present.
Exceptions:
- None.
Template Parameters:
- F Callable type.
Return: value or result of f(error).
function transform¶
Monadic operation: transforms the value.
Parameters:
- f Function to apply to value.
Exceptions:
- None.
Template Parameters:
- F Callable type.
Return: expected with transformed value or error.
function transform¶
Monadic operation: transforms the value (const).
Parameters:
- f Function to apply to value.
Exceptions:
- None.
Template Parameters:
- F Callable type.
Return: expected with transformed value or error.
function transform¶
Monadic operation: transforms the value (rvalue).
Parameters:
- f Function to apply to value.
Exceptions:
- None.
Template Parameters:
- F Callable type.
Return: expected with transformed value or error.
function transform_error¶
Monadic operation: transforms the error.
Parameters:
- f Function to apply to error.
Exceptions:
- None.
Template Parameters:
- F Callable type.
Return: expected with value or transformed error.
function transform_error¶
Monadic operation: transforms the error (const).
Parameters:
- f Function to apply to error.
Exceptions:
- None.
Template Parameters:
- F Callable type.
Return: expected with value or transformed error.
function transform_error¶
Monadic operation: transforms the error (rvalue).
Parameters:
- f Function to apply to error.
Exceptions:
- None.
Template Parameters:
- F Callable type.
Return: expected with value or transformed error.
function swap¶
Swaps the contents with another expected.
Parameters:
- o Expected to swap with.
Exceptions:
- None if types are nothrow move constructible and swappable.
Since: 0.1
Note: None.
Warning: None.
Friends¶
friend operator==¶
Equality comparison with another expected.
Parameters:
- a First expected.
- b Second expected.
Exceptions:
- None.
Template Parameters:
- T2 Other value type.
- E2 Other error type.
Return: true if equal, false otherwise.
friend operator==¶
Equality comparison with a value.
Parameters:
- a Expected.
- v Value to compare.
Exceptions:
- None.
Template Parameters:
- T2 Value type to compare with.
Return: true if expected contains value equal to v.
friend operator==¶
Equality comparison with unexpected.
Parameters:
- a Expected.
- u Unexpected to compare.
Exceptions:
- None.
Template Parameters:
- E2 Error type of unexpected.
Return: true if expected contains matching error.
friend operator!=¶
Inequality comparison with another expected.
Parameters:
- a First expected.
- b Second expected.
Exceptions:
- None.
Template Parameters:
- T2 Other value type.
- E2 Other error type.
Return: true if not equal, false otherwise.
friend operator!=¶
Inequality comparison with a value.
Parameters:
- a Expected.
- v Value to compare.
Exceptions:
- None.
Template Parameters:
- T2 Value type to compare with.
Return: true if not equal, false otherwise.
friend operator!=¶
Inequality comparison with unexpected.
Parameters:
- a Expected.
- u Unexpected to compare.
Exceptions:
- None.
Template Parameters:
- E2 Error type of unexpected.
Return: true if not equal, false otherwise.
friend swap¶
Non-member swap function.
Parameters:
- a First expected.
- b Second expected.
Exceptions:
- None if types are nothrow move constructible and swappable.
Since: 0.1
Note: None.
Warning: None.
Updated on 2026-03-09 at 10:14:00 +0000