std::optional::transform(3)
πΈ NAME πΈ
okay bestie, this is std::optional::transform(3) from the C++ Standard Libary β itβs the monadic glow-up your optional has been waiting for β¨
β¨ Synopsis β¨
template< class F > (1) (since C++23)
constexpr auto transform( F&& f ) &;
template< class F > (2) (since C++23)
constexpr auto transform( F&& f ) const&;
template< class F > (3) (since C++23)
constexpr auto transform( F&& f ) &&;
template< class F > (4) (since C++23)
constexpr auto transform( F&& f ) const&&;
If *this contains a value, it invokes f with the contained value as an argument, and returns an std::optional that contains the result of that invocation; otherwise, it returns an empty std::optional. π
The type of contained value in the result (weβre calling it 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. no cap, thatβs a hard error.
- Let
Ubestd::remove_cv_t<std::invoke_result_t<F, T&>>. If*thiscontains a value, returns astd::optional<U>whose contained value is direct-initialized fromstd::invoke(std::forward<F>(f), **this)(unlikeand_then(), which must return anstd::optionaldirectly). Otherwise, returns an emptystd::optional<U>. The program is ill-formed if the variable definitionU x(std::invoke(std::forward<F>(f), **this));is ill-formed. π - Same as (1), except that
Uisstd::remove_cv_t<std::invoke_result_t<F, const T&>>. - Let
Ubestd::remove_cv_t<std::invoke_result_t<F, T>>. If*thiscontains 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>. The program is ill-formed if the variable definitionU x(std::invoke(std::forward<F>(f), std::move(**this)));is ill-formed. π - Same as (3), except that
Uisstd::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. literally the callable bestie youβre handing the value to π«Ά
π Return value π
An std::optional containing the result of f or an empty std::optional, as described above. yep, thatβs the whole vibe check β
π¦ Notes π¦
- Because
transformdirectly constructs aUobject at the right location, rather than passing it to a constructor,std::is_move_constructible_v<U>can befalse. not me side-eyeing that but ok - As the callable
fcanβt return a reference type, it cannot be a pointer to data member. - Some languages call this operation
map. iykyk πΊοΈ - Feature-test macro:
__cpp_lib_optional - Value:
202110L - Std:
(C++23) - Feature:
Monadic operations in std::optional
π Example π
run this code and watch the chain go off π₯
// Run this code
#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 value
A => B
B => C
C => D
o_D has a value
o_A is empty
o_D is empty
π See also π
value_orβ returns the contained value if available, another value otherwise (public member function). πand_thenβ (C++23) returns the result of the given function on the contained value if it exists, or an empty optional otherwise (public member function). π¦or_elseβ (C++23) returns the optional itself if it contains a value, or the result of the given function otherwise (public member function). β¨
mwah π this page is from http://cppreference.com, dated 2024.06.10, and itβs std::optional::transform(3). bye bestie, go transform something iconic π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms