Skip to content

cvw::expected

A wrapper that contains either a value or an error. More...

#include <expected.hpp>

Public Types

Name
using Tvalue_type
Value type alias.
using Eerror_type
Error type alias.
using unexpected<E>unexpected_type
Unexpected type alias.

Public Functions

Name
constexprexpected()
Default constructor. Value-initializes T.
expected(constexpected & o)
Copy constructor.
expected(expected && o)
Move constructor.
template <typenameU =T&gt;
constexpr
expected(U && v)
Converting constructor from value.
template <typenameG =E&gt;
constexpr
expected(constunexpected<G> & u)
Constructor from unexpected.
template <typenameG =E&gt;
constexpr
expected(unexpected<G> && u)
Constructor from unexpected rvalue.
template <typename... Args&gt;
constexpr
expected(std::in_place_t , Args &&... args)
In-place constructor for value.
template <typename... Args&gt;
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 &gt;
expected &
operator=(U && v)
Assignment from value.
template <typenameG &gt;
expected &
operator=(constunexpected<G> & u)
Assignment from unexpected.
template <typenameG &gt;
expected &
operator=(unexpected<G> && u)
Assignment from unexpected rvalue.
constexprboolhas_value() const
Checks if the expected contains a value.
constexproperator 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 &gt;
constexprT
value_or(U && default_val) const
Returns the contained value or a default value.
template <typenameU &gt;
constexprT
value_or(U && default_val)
Returns the contained value or a default value (rvalue).
template <typenameU &gt;
constexprE
error_or(U && default_err) const
Returns the contained error or a default error.
template <typenameU &gt;
constexprE
error_or(U && default_err)
Returns the contained error or a default error (rvalue).
template <typenameF &gt;
auto
and_then(F && f)
Monadic operation: chains if value is present.
template <typenameF &gt;
auto
and_then(F && f) const
Monadic operation: chains if value is present (const).
template <typenameF &gt;
auto
and_then(F && f)
Monadic operation: chains if value is present (rvalue).
template <typenameF &gt;
auto
or_else(F && f)
Monadic operation: chains if error is present.
template <typenameF &gt;
auto
or_else(F && f) const
Monadic operation: chains if error is present (const).
template <typenameF &gt;
auto
or_else(F && f)
Monadic operation: chains if error is present (rvalue).
template <typenameF &gt;
auto
transform(F && f)
Monadic operation: transforms the value.
template <typenameF &gt;
auto
transform(F && f) const
Monadic operation: transforms the value (const).
template <typenameF &gt;
auto
transform(F && f)
Monadic operation: transforms the value (rvalue).
template <typenameF &gt;
auto
transform_error(F && f)
Monadic operation: transforms the error.
template <typenameF &gt;
auto
transform_error(F && f) const
Monadic operation: transforms the error (const).
template <typenameF &gt;
auto
transform_error(F && f)
Monadic operation: transforms the error (rvalue).
voidswap(expected & o)
Swaps the contents with another expected.

Friends

Name
constexprbooloperator==(constexpected & a, constexpected<T2, E2> & b)
Equality comparison with another expected.
constexprbooloperator==(constexpected & a, constT2 & v)
Equality comparison with a value.
constexprbooloperator==(constexpected & a, constunexpected<E2> & u)
Equality comparison with unexpected.
constexprbooloperator!=(constexpected & a, constexpected<T2, E2> & b)
Inequality comparison with another expected.
constexprbooloperator!=(constexpected & a, constT2 & v)
Inequality comparison with a value.
constexprbooloperator!=(constexpected & a, constunexpected<E2> & u)
Inequality comparison with unexpected.
voidswap(expected & a, expected & b)
Non-member swap function.

Detailed Description

cpp
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

cpp
using cvw::expected<T, E>::value_type =  T;

Value type alias.

using error_type

cpp
using cvw::expected<T, E>::error_type =  E;

Error type alias.

using unexpected_type

cpp
using cvw::expected<T, E>::unexpected_type =  unexpected<E>;

Unexpected type alias.

Public Functions Documentation

function expected

cpp
inline constexpr expected()

Default constructor. Value-initializes T.

Exceptions:

  • May throw if T default constructor throws.

function expected

cpp
inline expected(
    constexpected & o
)

Copy constructor.

Parameters:

  • o Expected to copy from.

Exceptions:

  • May throw if T or E copy constructor throws.

function expected

cpp
inline expected(
    expected && o
)

Move constructor.

Parameters:

  • o Expected to move from.

Exceptions:

  • May throw if T or E move constructor throws.

function expected

cpp
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

cpp
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

cpp
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

cpp
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

cpp
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

cpp
inline ~expected()

Destructor. Destroys contained value or error.

Exceptions:

  • None.

function operator=

cpp
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=

cpp
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=

cpp
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=

cpp
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=

cpp
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

cpp
inline constexprbool has_value() const

Checks if the expected contains a value.

Exceptions:

  • None.

Return: true if contains a value, false if contains error.

function operator bool

cpp
inline explicit constexpr operator bool() const

Boolean conversion operator.

Exceptions:

  • None.

Return: true if contains a value, false if contains error.

function operator->

cpp
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->

cpp
inline constexprconstT * operator->() const

Const pointer operator.

Exceptions:

  • None.

Return: Pointer to the contained value.

Warning: Undefined behavior if expected does not contain a value.

function operator*

cpp
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*

cpp
inline constexprconstT & operator*() const

Const dereference operator.

Exceptions:

  • None.

Return: Const reference to the contained value.

Warning: Undefined behavior if expected does not contain a value.

function operator*

cpp
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*

cpp
inline constexprconstT && operator*() const

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

cpp
inline constexprT & value()

Returns the contained value.

Exceptions:

Return: Reference to the contained value.

Warning: Throws bad_expected_access if this expected does not contain a value.

function value

cpp
inline constexprconstT & value() const

Returns the contained value (const lvalue).

Exceptions:

Return: Const reference to the contained value.

Warning: Throws bad_expected_access if this expected does not contain a value.

function value

cpp
inline constexprT && value()

Returns the contained value (rvalue).

Exceptions:

Return: Rvalue reference to the contained value.

Warning: Throws bad_expected_access if this expected does not contain a value.

function value

cpp
inline constexprconstT && value() const

Returns the contained value (const rvalue).

Exceptions:

Return: Const rvalue reference to the contained value.

Warning: Throws bad_expected_access if this expected does not contain a value.

function error

cpp
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

cpp
inline constexprconstE & error() const

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

cpp
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

cpp
inline constexprconstE && error() const

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

cpp
template <typenameU>
inline constexprT value_or(
    U && default_val
) const

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

cpp
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

cpp
template <typenameU>
inline constexprE error_or(
    U && default_err
) const

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

cpp
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

cpp
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

cpp
template <typenameF>
inline auto and_then(
    F && f
) const

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

cpp
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

cpp
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

cpp
template <typenameF>
inline auto or_else(
    F && f
) const

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

cpp
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

cpp
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

cpp
template <typenameF>
inline auto transform(
    F && f
) const

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

cpp
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

cpp
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

cpp
template <typenameF>
inline auto transform_error(
    F && f
) const

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

cpp
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

cpp
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==

cpp
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==

cpp
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==

cpp
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!=

cpp
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!=

cpp
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!=

cpp
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

cpp
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

Built with VitePress