std::expected::and_then(3)
π
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>>ifTis (possibly cv-qualified)void, orstd::remove_cvref_t<std::invoke_result_t<F, decltype(**this)>>otherwise.
π¨ IfUis not a specialization ofstd::expected, orstd::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>>ifTis (possibly cv-qualified)void, orstd::remove_cvref_t<std::invoke_result_t<F, decltype(std::move(**this))>>otherwise.
π¨ IfUis not a specialization ofstd::expected, orstd::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 astd::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
- Value:
π§ 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_thenwas ill-formed ifTis not (possibly cv-qualified)voidandEis not copyable. - Correct behavior: made well-formed.
- Applied to:
π See also π
unexpect,unexpect_tβ in-place construction tag for unexpected value inexpected(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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms