cvw::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> constexpr | expected(U && v) Converting constructor from value. |
| template <typenameG =E> constexpr | expected(constunexpected<G> & u) Constructor from unexpected. |
| template <typenameG =E> constexpr | expected(unexpected<G> && u) Constructor from unexpected rvalue. |
| template <typename... Args> constexpr | expected(std::in_place_t , Args &&... args) In-place constructor for value. |
| template <typename... Args> 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 > 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
template <typenameT ,
typenameE >
class cvw::expected;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
using cvw::expected<T, E>::value_type = T;Value type alias.
using error_type
using cvw::expected<T, E>::error_type = E;Error type alias.
using unexpected_type
using cvw::expected<T, E>::unexpected_type = unexpected<E>;Unexpected type alias.
Public Functions Documentation
function expected
inline constexpr expected()Default constructor. Value-initializes T.
Exceptions:
- May throw if T default constructor throws.
function expected
inline expected(
constexpected & o
)Copy constructor.
Parameters:
- o Expected to copy from.
Exceptions:
- May throw if T or E copy constructor throws.
function expected
inline expected(
expected && o
)Move constructor.
Parameters:
- o Expected to move from.
Exceptions:
- May throw if T or E move constructor throws.
function expected
template <typenameU =T>
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>
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>
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>
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>
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
inline ~expected()Destructor. Destroys contained value or error.
Exceptions:
- None.
function operator=
inline expected & operator=(
constexpected & o
)Copy assignment operator.
Parameters:
- o Expected to copy from.
Exceptions:
- May throw if assignment or construction throws.
Return: Reference to this expected.
function operator=
inline expected & operator=(
expected && o
)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 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=
template <typenameG>
inline expected & operator=(
constunexpected<G> & u
)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=
template <typenameG>
inline expected & operator=(
unexpected<G> && u
)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
inline constexprbool has_value() constChecks if the expected contains a value.
Exceptions:
- None.
Return: true if contains a value, false if contains error.
function operator bool
inline explicit constexpr operator bool() constBoolean conversion operator.
Exceptions:
- None.
Return: true if contains a value, false if contains error.
function operator->
inline constexprT * operator->()Pointer operator.
Exceptions:
- None.
Return: Pointer to the contained value.
Warning: Undefined behavior if expected does not contain a value.
function operator->
inline constexprconstT * operator->() constConst pointer operator.
Exceptions:
- None.
Return: Pointer to the contained value.
Warning: Undefined behavior if expected does not contain a value.
function operator*
inline constexprT & operator*()Dereference operator.
Exceptions:
- None.
Return: Reference to the contained value.
Warning: Undefined behavior if expected does not contain a value.
function operator*
inline constexprconstT & operator*() constConst dereference operator.
Exceptions:
- None.
Return: Const reference to the contained value.
Warning: Undefined behavior if expected does not contain a value.
function operator*
inline constexprT && 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*
inline constexprconstT && operator*() constConst 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
inline constexprT & 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
inline constexprconstT & value() constReturns 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
inline constexprT && 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
inline constexprconstT && value() constReturns 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
inline constexprE & 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
inline constexprconstE & error() constReturns 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
inline constexprE && 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
inline constexprconstE && error() constReturns 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
template <typenameU>
inline constexprT value_or(
U && default_val
) constReturns 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
template <typenameU>
inline constexprT value_or(
U && default_val
)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
template <typenameU>
inline constexprE error_or(
U && default_err
) constReturns 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
template <typenameU>
inline constexprE error_or(
U && default_err
)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
template <typenameF>
inline auto and_then(
F && f
)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
template <typenameF>
inline auto and_then(
F && f
) constMonadic 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
template <typenameF>
inline auto and_then(
F && f
)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
template <typenameF>
inline auto or_else(
F && f
)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
template <typenameF>
inline auto or_else(
F && f
) constMonadic 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
template <typenameF>
inline auto or_else(
F && f
)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
template <typenameF>
inline auto transform(
F && f
)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
template <typenameF>
inline auto transform(
F && f
) constMonadic 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
template <typenameF>
inline auto transform(
F && f
)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
template <typenameF>
inline auto transform_error(
F && f
)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
template <typenameF>
inline auto transform_error(
F && f
) constMonadic 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
template <typenameF>
inline auto transform_error(
F && f
)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
inline void swap(
expected & o
)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==
friend constexprbool operator==(
constexpected & a,
constexpected<T2, E2> & b
);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==
friend constexprbool operator==(
constexpected & a,
constT2 & v
);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==
friend constexprbool operator==(
constexpected & a,
constunexpected<E2> & u
);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!=
friend constexprbool operator!=(
constexpected & a,
constexpected<T2, E2> & b
);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!=
friend constexprbool operator!=(
constexpected & a,
constT2 & v
);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!=
friend constexprbool operator!=(
constexpected & a,
constunexpected<E2> & u
);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
friend void swap(
expected & a,
expected & b
);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-05-17 at 13:22:38 +0000