|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Observers | ||||
| Iterators | ||||
(C++26) | ||||
(C++26) | ||||
| Monadic operations | ||||
optional::and_then (C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Modifiers | ||||
| Non-member functions | ||||
| Deduction guides | ||||
| Helper classes | ||||
| Helper objects | ||||
template<class F> constexprauto and_then( F&& f)&; | (1) | (since C++23) |
template<class F> constexprauto and_then( F&& f)const&; | (2) | (since C++23) |
template<class F> constexprauto and_then( F&& f)&&; | (3) | (since C++23) |
template<class F> constexprauto and_then( F&& f)const&&; | (4) | (since C++23) |
If*this contains a value, invokesf with the contained value as an argument, and returns the result of that invocation; otherwise, returns an emptystd::optional.
The return type (see below) must be a specialization ofstd::optional (unliketransform()). Otherwise, the program is ill-formed.
if(*this)returnstd::invoke(std::forward<F>(f), value());elsereturnstd::remove_cvref_t<std::invoke_result_t<F, T&>>{};
if(*this)returnstd::invoke(std::forward<F>(f), value());elsereturnstd::remove_cvref_t<std::invoke_result_t<F,const T&>>{};
if(*this)returnstd::invoke(std::forward<F>(f), std::move(value()));elsereturnstd::remove_cvref_t<std::invoke_result_t<F, T>>{};
if(*this)returnstd::invoke(std::forward<F>(f), std::move(value());elsereturnstd::remove_cvref_t<std::invoke_result_t<F,const T>>{};
Contents |
| f | - | a suitable function orCallable object that returns anstd::optional |
The result off or an emptystd::optional, as described above.
Some languages call this operationflatmap.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_optional | 202110L | (C++23) | Monadic operations instd::optional |
#include <charconv>#include <iomanip>#include <iostream>#include <optional>#include <ranges>#include <string>#include <string_view>#include <vector> std::optional<int> to_int(std::string_view sv){int r{};auto[ptr, ec]{std::from_chars(sv.data(), sv.data()+ sv.size(), r)};if(ec==std::errc())return r;elsereturnstd::nullopt;} int main(){usingnamespace std::literals; conststd::vector<std::optional<std::string>> v{"1234","15 foo","bar","42","5000000000"," 5",std::nullopt,"-43"}; for(auto&& x: v| std::views::transform([](auto&& o){// debug print the content of input optional<string>std::cout<<std::left<<std::setw(13)<<std::quoted(o.value_or("nullopt"))<<" -> "; return o// if optional is nullopt convert it to optional with "" string .or_else([]{returnstd::optional{""s};})// flatmap from strings to ints (making empty optionals where it fails) .and_then(to_int)// map int to int + 1 .transform([](int n){return n+1;})// convert back to strings .transform([](int n){returnstd::to_string(n);})// replace all empty optionals that were left by// and_then and ignored by transforms with "NaN" .value_or("NaN"s);}))std::cout<< x<<'\n';}
Output:
"1234" -> 1235"15 foo" -> 16"bar" -> NaN"42" -> 43"5000000000" -> NaN" 5" -> NaN"nullopt" -> NaN"-43" -> -42
| returns the contained value if available, another value otherwise (public member function)[edit] | |
(C++23) | returns anoptional containing the transformed 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] |