Namespaces
Variants
Views
Actions

std::optional<T>::transform

From cppreference.com
< cpp‎ | utility‎ | optional
 
 
Utilities library
General utilities
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)

Elementary string conversions
(C++17)
(C++17)

Stacktrace
 
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>>.

Contents

[edit] Parameters

f - a suitable function or Callable object

[edit] Return value

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

[edit] 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.

Some languages call this operation map.

[edit] Example

[edit] See also

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