std::expect...sform_error(3)
π NAME π
Okay bestie, welcome to the std::expect...sform_error(3) page from the C++ Standard Libary manual β¨ (yes, that's the manual name, typo and all β we keep it iconic). This page is about std::expected::transform_error, and the NAME line is literally serving double duty: std::expected::transform_error β std::expected::transform_error. Periodt. π
π¦ Synopsis π¦
Four overloads, all constexpr auto, all since C++23, each with a different ref-qualifier. The exact lineup is giving:
template< class F > (1) (since C++23)
constexpr auto transform_error( F&& f ) &;
template< class F > (2) (since C++23)
constexpr auto transform_error( F&& f ) const&;
template< class F > (3) (since C++23)
constexpr auto transform_error( F&& f ) &&;
template< class F > (4) (since C++23)
constexpr auto transform_error( F&& f ) const&&;
Here's the vibe check on the behavior, no cap: if *this contains an error value, it invokes f with the argument error() and returns a std::expected object that contains its result; otherwise, it returns a std::expected object that contains a copy of the contained expected value (obtained from operator*). π«Ά
1,2) Given type G as std::remove_cv_t<std::invoke_result_t<F, decltype(error())>>. If G is not a valid template argument for std::unexpected, or G g(std::invoke(std::forward<F>(f), error())); is ill-formed, the program is ill-formed. The effect is equivalent to:
if (has_value())
{
if (std::is_void_v<T>)
return std::expected<T, G>();
else
return std::expected<T, G>(std::in_place, **this);
}
else
// the returned std::expected object contains an unexpected value,
// which is direct-non-list-initialized with
// std::invoke(std::forward<F>(f), error())
return /* an std::expected<T, G> object */;
These overloads participate in overload resolution only if std::is_void_v<T> or std::is_constructible_v<T, decltype(**this)> is true. β¨
3,4) Given type G as std::remove_cv_t<std::invoke_result_t<F, decltype(std::move(error()))>>. If G is not a valid template argument for std::unexpected, or G g(std::invoke(std::forward<F>(f), std::move(error()))); is ill-formed, the program is ill-formed. The effect is equivalent to:
if (has_value())
{
if (std::is_void_v<T>)
return std::expected<T, G>();
else
return std::expected<T, G>(std::in_place, std::move(**this));
}
else
// the returned std::expected object contains an unexpected value,
// which is direct-non-list-initialized with
// std::invoke(std::forward<F>(f), std::move(error()))
return /* an std::expected<T, G> object */;
These overloads participate in overload resolution only if std::is_void_v<T> or std::is_constructible_v<T, decltype(std::move(**this))> 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 expected value, as described above. π
π§ Example π§
This section is incomplete.
Reason: no example. Honestly? Same, bestie. π
π₯ Defect reports π₯
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. No cap, the standards got patched after the fact:
LWG 3938β Applied to:C++23; Behavior as published:transform_errorwas ill-formed ifTis not (possibly cv-qualified)voidandEis not copyable; Correct behavior: made well-formed.
π«Ά See also π«Ά
or_elseβ returns the expected itself if it contains an expected value; otherwise, returns the result of the given function on the unexpected value (public member function)transformβ returns an expected containing the transformed expected value if it exists; otherwise, returns the expected itself (public member function)
π Category π
Todo no example
Shoutout to http://cppreference.com, the 2024.06.10 version/date, and this whole std::expect...sform_error(3) page β mwah πβ¨
π¦ 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