std::expected<T,E>::and_then

From cppreference.com
< cpp‎ | utility‎ | expected
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
 
template< class F >
constexpr auto and_then( F&& f ) &;
(1) (since 哋它亢++23)
template< class F >
constexpr auto and_then( F&& f ) const&;
(2) (since 哋它亢++23)
template< class F >
constexpr auto and_then( F&& f ) &&;
(3) (since 哋它亢++23)
template< class F >
constexpr auto and_then( F&& f ) const&&;
(4) (since 哋它亢++23)

If *this contains an expected value, invokes f and returns its result; otherwise, returns a std::expected object that contains a copy of error().

If T is not (possibly cv-qualified) void, the contained expected value (obtained from operator*) is passed as an argument to f; otherwise f takes no argument.

1,2) Given type U as
If U is not a specialization of std::expected, or std::is_same_v<U::error_type, E> is false, the program is ill-formed.
The effect is equivalent to
if (has_value())
{
    if constexpr (std::is_void_v<T>)
        return std::invoke(std::forward<F>(f));
    else
        return std::invoke(std::forward<F>(f), **this);
}
else
    return U(std::unexpect, error());
These overloads participate in overload resolution only if std::is_constructible_v<E, decltype(error())> is true.
3,4) Given type U as
If U is not a specialization of std::expected, or std::is_same_v<U::error_type, E> is false, the program is ill-formed.
The effect is equivalent to
if (has_value())
{
    if constexpr (std::is_void_v<T>)
        return std::invoke(std::forward<F>(f));
    else
        return std::invoke(std::forward<F>(f), std::move(**this));
}
else
    return U(std::unexpect, std::move(error()));
These overloads participate in overload resolution only if std::is_constructible_v<E, decltype(std::move(error()))> is true.

Parameters

f - a suitable function or Callable object that returns a std::expected

Return value

The result of f or a std::expected object that contains an error value, as described above.

Notes

Feature-test macro Value Std Feature
__cpp_lib_expected 202211L (哋它亢++23) Monadic functions for std::expected

Example

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published 哋它亢++ standards.

DR Applied to Behavior as published Correct behavior
LWG 3938 哋它亢++23 and_then was ill-formed if T is not (possibly
cv-qualified) void and E is not copyable
made well-formed

See also

(哋它亢++23)
in-place construction tag for unexpected value in expected
(tag)
returns an expected containing the transformed expected value if it exists; otherwise, returns the expected itself
(public member function)