std::expected::transform(3)
π std::expected::transform(3) π
okay bestie, welcome to the std::expected::transform(3) page from the C++ Standard Libary (yes, that's how the header spells it, iykyk π). This is the C++23 glow-up for transforming an expected value, and it's giving main-character energy fr fr. Let's get into it π
β¨
πΈ NAME πΈ
std::expected::transform β std::expected::transform
β¨ Synopsis β¨
template< class F > (1) (since C++23)
constexpr auto transform( F&& f ) &;
template< class F > (2) (since C++23)
constexpr auto transform( F&& f ) const&;
template< class F > (3) (since C++23)
constexpr auto transform( F&& f ) &&;
template< class F > (4) (since C++23)
constexpr auto transform( F&& f ) const&&;
okay so here's the vibe: if *this contains an expected value, it invokes f and returns a std::expected object that contains its result. otherwise, it returns a std::expected object that contains a copy of error(). literally that simple, no cap π
if T is not (possibly cv-qualified) void, the contained value (obtained from operator*) is passed as an argument to f; otherwise f takes no argument.
1,2) Given type U as:
std::remove_cv_t<std::invoke_result_t<F>>ifTis (possibly cv-qualified)void, orstd::remove_cv_t<std::invoke_result_t<F, decltype(**this)>>otherwise,
and expression /* invoke-expr */ as:
std::invoke(std::forward<F>(f))ifTis (possibly cv-qualified)void, orstd::invoke(std::forward<F>(f), **this)otherwise.
If U is not a valid value type for std::expected, or U u(/* invoke-expr */); is ill-formed when std::is_void_v<U> is false, the program is ill-formed.
The effect is equivalent to:
if (has_value())
{
if constexpr (std::is_void_v<U>)
{
/* invoke-expr */;
return std::expected<U, E>();
}
else
// the returned std::expected object contains an expected value,
// which is direct-non-list-initialized with /* invoke-expr */
return /* an std::expected<U, E> object */;
}
else
return std::expected<U, E>(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_cv_t<std::invoke_result_t<F>>ifTis (possibly cv-qualified)void, orstd::remove_cv_t<std::invoke_result_t<F, decltype(std::move(**this))>>otherwise,
and expression /* invoke-expr */ as:
std::invoke(std::forward<F>(f))ifTis (possibly cv-qualified)void, orstd::invoke(std::forward<F>(f), std::move(**this))otherwise.
If U is not a valid value type for std::expected, or U u(/* invoke-expr */); is ill-formed when std::is_void_v<U> is false, the program is ill-formed.
The effect is equivalent to:
if (has_value())
{
if constexpr (std::is_void_v<U>)
{
/* invoke-expr */;
return std::expected<U, E>();
}
else
// the returned std::expected object contains an expected value,
// which is direct-non-list-initialized with /* invoke-expr */
return /* an std::expected<U, E> object */;
}
else
return std::expected<U, E>(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 whose call signature returns a non-reference type.
π¦ Return value π¦
A std::expected object containing either the result of f or an error value, as described above.
π Example π
This section is incomplete Reason: no example
bestie there is literally no example here π the page says so itself, not me.
π₯ 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:transformwas ill-formed ifTis not (possibly cv-qualified)voidandEis not copyable; Correct behavior: made well-formed.
π See also π
transform_errorβ returns the expected itself if it contains an expected value; otherwise, returns an expected containing the transformed unexpected value (public member function).
π Category π
Todo no example
mwah π this has been std::expected::transform(3) from the C++ Standard Libary, source http://cppreference.com, page dated 2024.06.10. go transform something bestie π«Άβ¨
π¦ 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