C++23 adds some "monadic-style" functionality regarding optionals, as methods of optional<T>
:
optional<T>::and_then()
(and ignoring qualifiers of this
):
template<class F> constexpr auto and_then(F&& f);
Returns the result of invocation of f on the contained value if it exists. Otherwise, returns an empty value of the return type.
optional<T>::transform()
(and ignoring qualifiers of this
):
template<class F> constexpr auto transform(F&& f);
Returns an
std::optional
that contains the result of invocation off
on the contained value if*this
contains a value. Otherwise, returns an emptystd::optional
of such type.
So, aren't these two functions doing the same thing?