Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | utility‎ | expected
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
(C++26)
Three-way comparison
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
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)

 
 
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 (obtained from operator*) is passed as an argument to f; otherwise f takes no argument.

1,2) Given type U as
and expression /* invoke-expr */ as
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
and expression /* invoke-expr */ as
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.

Contents

[edit] Parameters

f - a suitable function or Callable object whose call signature returns a non-reference type

[edit] Return value

A std::expected object containing either the result of f or an error value, as described above.

[edit] Example

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 3938 C++23 transform was ill-formed if T is not (possibly
cv-qualified) void and E is not copyable
made well-formed

[edit] See also

returns the expected itself if it contains an expected value; otherwise, returns an expected containing the transformed unexpected value
(public member function) [edit]