std::optional<T>::transform

From cppreference.com
< cpp‎ | utility‎ | optional
 
 
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::optional
Member functions
Observers
Monadic operations
optional::transform
(C++23)
Modifiers
Non-member functions
(C++17)(C++17)(C++17)(C++17)(C++17)(C++17)(C++20)
(C++17)
Deduction guides
Helper classes
(C++17)
(C++17)
(C++17)
Helper objects
(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)

Returns an std::optional that contains the result of invocation of f on the contained value if *this contains a value. Otherwise, returns an empty std::optional of such type.

The type of contained value in the result (denoted by U below) must be a non-array object type, and must not be std::in_place_t or std::nullopt_t. Otherwise, the program is ill-formed.

1) Let U be std::remove_cv_t<std::invoke_result_t<F, T&>>. If *this contains a value, returns a std::optional<U> whose contained value is directly-non-list-initialized from std::invoke(std::forward<F>(f), this->value()). Otherwise, returns an empty std::optional<U>.
The program is ill-formed if the variable definition U x(std::invoke(std::forward<F>(f), this->value())); is ill-formed.
2) Same as (1), except that U is std::remove_cv_t<std::invoke_result_t<F, const T&>>.
3) Let U be std::remove_cv_t<std::invoke_result_t<F, T>>. If *this contains a value, returns a std::optional<U> whose contained value is directly-non-list-initialized from std::invoke(std::forward<F>(f), std::move(this->value())). Otherwise, returns an empty std::optional<U>.
The program is ill-formed if the variable definition U x(std::invoke(std::forward<F>(f), std::move(this->value()))); is ill-formed.
4) Same as (3), except that U is std::remove_cv_t<std::invoke_result_t<F, const T>>.

Parameters

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

Return value

An std::optional containing the result of f or an empty std::optional, as described above.

Notes

Because transform directly constructs a U object at the right location, rather than passing it to a constructor, std::is_move_constructible_v<U> can be false.

As the callable f can't return a reference type, it cannot be a pointer to data member.

Some languages call this operation map.

Feature-test macro: __cpp_lib_monadic_optional

Example

#include <iostream>
#include <optional>
 
struct A { friend std::ostream& operator<< (std::ostream& os, A) { return os << 'A'; } };
struct B { friend std::ostream& operator<< (std::ostream& os, B) { return os << 'B'; } };
struct C { friend std::ostream& operator<< (std::ostream& os, C) { return os << 'C'; } };
struct D { friend std::ostream& operator<< (std::ostream& os, D) { return os << 'D'; } };
 
auto A_to_B(A in) { B out; std::cout << in << " => " << out << '\n'; return out; }
auto B_to_C(B in) { C out; std::cout << in << " => " << out << '\n'; return out; }
auto C_to_D(C in) { D out; std::cout << in << " => " << out << '\n'; return out; }
 
int main()
{
    for (std::optional<A> o_A : { std::optional<A>{ A{} }, std::optional<A>{/*empty*/} })
    {
        std::cout << (o_A ? "o_A has a value\n" : "o_A is empty\n");
 
        std::optional<D> o_D = o_A.transform(A_to_B)
                                  .transform(B_to_C)
                                  .transform(C_to_D);
 
        std::cout << (o_D ? "o_D has a value\n\n" : "o_D is empty\n\n");
    }
}

Output:

o_A has a value
A => B
B => C
C => D
o_D has a value
 
o_A is empty
o_D is empty

See also

returns the contained value if available, another value otherwise
(public member function)
(C++23)
returns the result of the given function on the contained value if it exists, or an empty optional otherwise
(public member function)
(C++23)
returns the optional itself if it contains a value, or the result of the given function otherwise
(public member function)