# std::expected<T,E>::transform

From cppreference.com
< cpp‎ | utility‎ | expected

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

If *this contains an expected value, invokes f and returns a `std::expected` object that contains its result; otherwise, returns a `std::expected` object that contains a copy of `error()`.

If `T` is not (possibly cv-qualified) void, the contained value (`**this`) is passed as an argument to f; otherwise f takes no argument.

Let `U` be:

- if `T` is not (possibly cv-qualified) void:
  - for overloads (1-2), std::remove_cv_t<std::invoke_result_t<F, decltype(**this)>>;
  - for overloads (3-4), std::remove_cv_t<std::invoke_result_t<F, decltype(std::move(**this))>>;
- otherwise (T is possibly cv-qualified void), std::remove_cv_t<std::invoke_result_t<F>>.

`U` must be a valid value type for `std::expected`. A variable of type `U` must be constructible from the result of invocation (but does not need to be move-constructible). The return type is std::expected<U, E>.

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

1. Invokes f as if by
   - std::invoke(std::forward<F>(f), **this) if std::is_void_v<T> is false;
   - std::invoke(std::forward<F>(f)) otherwise.
2. Then
   - if std::is_void_v<U> is false, returns a `std::expected` object that contains an expected value, direct-initialized from the result of invocation;
   - otherwise, returns std::expected<U, E>().

Otherwise (*this contains an error value), returns 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) If *this contains an expected value:

1. Invokes f as if by
   - std::invoke(std::forward<F>(f), std::move(**this)) if std::is_void_v<T> is false;
   - std::invoke(std::forward<F>(f)) otherwise.
2. Then
   - if std::is_void_v<U> is false, returns a `std::expected` object that contains an expected value, direct-initialized from the result of invocation;
   - otherwise, returns std::expected<U, E>().

Otherwise (*this contains an error value), returns 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

### See also

transform_error (C++23) | returns the `expected` itself if it contains an expected value; otherwise, returns an `expected` containing the transformed unexpected value (public member function)

Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/expected/transform&oldid=155021"