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

From cppreference.com
< cpp‎ | utility‎ | expected
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
std::expected
Member functions
Observers
Monadic operations
expected::transform_error
(C++23)
Modifiers
Non-member functions
(C++23)
(C++23)
Helper classes
(C++23)
(C++23)(C++23)
 
template< class F >
constexpr auto transform_error( F&& f ) &;
(1) (since C++23)
template< class F >
constexpr auto transform_error( F&& f ) const&;
(2) (since C++23)
template< class F >
constexpr auto transform_error( F&& f ) &&;
(3) (since C++23)
template< class F >
constexpr auto transform_error( F&& f ) const&&;
(4) (since C++23)

If *this contains an error value, invokes f and returns a std::expected object that contains its result; otherwise, returns a std::expected object that contains a copy of **this. The contained value (error()) is passed as an argument to f.

Let G be:

G must be a valid template argument for std::unexpected. A variable of type G must be constructible from the result of invocation (but does not need to be move-constructible). The return type is std::expected<T, G>.

1-2) If *this contains an expected value:

Otherwise (*this contains an error value), returns a std::expected<T, G> object that contains an error value, direct-initialized from std::invoke(std::forward<F>(f), error()).

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) If *this contains an expected value:
  • if std::is_void_v<T> is false, returns std::expected<T, G>(std::in_place, std::move(**this));
  • otherwise, returns std::expected<T, G>().

Otherwise (*this contains an error value), returns a std::expected<T, G> object that contains an error value, direct-initialized from std::invoke(std::forward<F>(f), std::move(error())).

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

See also

(C++23)
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)
(C++23)
returns an expected containing the transformed expected value if it exists; otherwise, returns the expected itself
(public member function)