std::optional<T>::and_then
From cppreference.com
template< class F > constexpr auto and_then( F&& f ) &; |
(1) | (since C++23) |
template< class F > constexpr auto and_then( F&& f ) const&; |
(2) | (since C++23) |
template< class F > constexpr auto and_then( F&& f ) &&; |
(3) | (since C++23) |
template< class F > constexpr auto and_then( F&& f ) const&&; |
(4) | (since C++23) |
Returns the result of invocation of f
on the contained value if it exists. Otherwise, returns an empty value of the return type.
The return type (see below) must be a specialization of std::optional. Otherwise, the program is ill-formed.
1) Equivalent to
if (*this)
if (*this)
return std::invoke(std::forward<F>(f), this->value());
else
2) Equivalent to
if (*this)
if (*this)
return std::invoke(std::forward<F>(f), this->value());
else
3) Equivalent to
if (*this)
if (*this)
return std::invoke(std::forward<F>(f), std::move(this->value()));
else
4) Equivalent to
if (*this)
if (*this)
return std::invoke(std::forward<F>(f), std::move(this->value()));
else
Parameters
f | - | a suitable function or Callable object that returns an std::optional |
Return value
The result of f
or an empty std::optional, as described above.
Notes
Some languages call this operation flatmap.
Feature testing macro: __cpp_lib_monadic_optional
Example
This section is incomplete Reason: no example |
See also
returns the contained value if available, another value otherwise (public member function) | |
(C++23) |
returns an optional containing the transformed 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) |