std::expected::and_then(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::expected::and_then(3) β€” bestie edition πŸŽ€

Straight from the C++ Standard Libary manual (yes, β€œLibary,” iconic, I don't make the rules πŸ’€). Okay so we're doing monadic std::expected vibes, no cap.

🌸 NAME 🌸

std::expected::and_then β€” std::expected::and_then

✨ Synopsis ✨

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

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

  • std::remove_cvref_t<std::invoke_result_t<F>> if T is (possibly cv-qualified) void, or
  • std::remove_cvref_t<std::invoke_result_t<F, decltype(**this)>> otherwise.
🚨 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

  • std::remove_cvref_t<std::invoke_result_t<F>> if T is (possibly cv-qualified) void, or
  • std::remove_cvref_t<std::invoke_result_t<F, decltype(std::move(**this))>> otherwise.
🚨 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 __cpp_lib_expected
    • Value: 202211L
    • Std: (C++23)
    • Feature: Monadic functions for std::expected

🚧 Example 🚧

This section is incomplete. Reason: no example.

πŸ”₯ Defect reports πŸ”₯

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

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

πŸ‘‘ See also πŸ‘‘

  • unexpect, unexpect_t β€” in-place construction tag for unexpected value in expected (tag) (C++23)
  • transform β€” returns an expected containing the transformed expected value if it exists; otherwise, returns the expected itself (public member function)

🏷️ Category 🏷️

  • Todo no example

xoxo, cppreference.com β€” version 2024.06.10 β€” std::expected::and_then(3) πŸ’–βœ¨πŸ«Ά

πŸ“¦ where this page comes from