|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Observers | ||||
| Iterators | ||||
(C++26) | ||||
(C++26) | ||||
| Monadic operations | ||||
(C++23) | ||||
optional::transform (C++23) | ||||
(C++23) | ||||
| Modifiers | ||||
| Non-member functions | ||||
| Deduction guides | ||||
| Helper classes | ||||
| Helper objects | ||||
template<class F> constexprauto transform( F&& f)&; | (1) | (since C++23) |
template<class F> constexprauto transform( F&& f)const&; | (2) | (since C++23) |
template<class F> constexprauto transform( F&& f)&&; | (3) | (since C++23) |
template<class F> constexprauto transform( F&& f)const&&; | (4) | (since C++23) |
If*this contains a value, invokesf with the contained value as an argument, and returns anstd::optional that contains the result of that invocation; otherwise, returns an emptystd::optional.
The type of contained value in the result (denoted byU below) must be a non-array object type, and must not bestd::in_place_t orstd::nullopt_t). Otherwise, the program is ill-formed.
U bestd::remove_cv_t<std::invoke_result_t<F, T&>>. If*this contains a value, returns astd::optional<U> whose contained value isdirect-initialized fromstd::invoke(std::forward<F>(f),**this) (unlikeand_then(), which must return anstd::optional directly). Otherwise, returns an emptystd::optional<U>.U bestd::remove_cv_t<std::invoke_result_t<F, T>>. If*this contains a value, returns astd::optional<U> whose contained value is direct-initialized fromstd::invoke(std::forward<F>(f), std::move(**this)). Otherwise, returns an emptystd::optional<U>.Contents |
| f | - | a suitable function orCallable object whose call signature returns a non-reference type |
Anstd::optional containing the result off or an emptystd::optional, as described above.
Becausetransform directly constructs aU object at the right location, rather than passing it to a constructor,std::is_move_constructible_v<U> can befalse.
As the callablef can't return a reference type, it cannot be apointer to data member.
Some languages call this operationmap.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_optional | 202110L | (C++23) | Monadic operations instd::optional |
#include <iostream>#include <optional> struct A{/* ... */};struct B{/* ... */};struct C{/* ... */};struct D{/* ... */}; auto A_to_B(A)-> B{/* ... */std::cout<<"A => B\n";return{};}auto B_to_C(B)-> C{/* ... */std::cout<<"B => C\n";return{};}auto C_to_D(C)-> D{/* ... */std::cout<<"C => D\n";return{};} void try_transform_A_to_D(std::optional<A> o_A){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");}; int main(){ try_transform_A_to_D( A{}); try_transform_A_to_D({});}
Output:
o_A has a valueA => BB => CC => Do_D has a value o_A is emptyo_D is empty
| 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 emptyoptional otherwise(public member function)[edit] |
(C++23) | returns theoptional itself if it contains a value, or the result of the given function otherwise(public member function)[edit] |