std::optional<T>::and_then
Von cppreference.com
| template< class F > constexpr auto and_then( F&& f ) &; |
(1) | (seit C++23) |
| template< class F > constexpr auto and_then( F&& f ) const&; |
(2) | (seit C++23) |
| template< class F > constexpr auto and_then( F&& f ) &&; |
(3) | (seit C++23) |
| template< class F > constexpr auto and_then( F&& f ) const&&; |
(4) | (seit C++23) |
Wenn *this einen Wert enthält, wird f mit dem enthaltenen Wert als Argument aufgerufen und das Ergebnis dieses Aufrufs zurückgegeben; andernfalls wird ein leeres std::optional zurückgegeben.
Der Rückgabetyp (siehe unten) muss eine Spezialisierung von std::optional sein (im Gegensatz zu transform()). Andernfalls ist das Programm schlecht geformt.
1) Äquivalent zu
if (*this) return std::invoke(std::forward<F>(f), value()); else return std::remove_cvref_t<std::invoke_result_t<F, T&>>{};
2) Äquivalent zu
if (*this) return std::invoke(std::forward<F>(f), value()); else return std::remove_cvref_t<std::invoke_result_t<F, const T&>>{};
3) Äquivalent zu
if (*this) return std::invoke(std::forward<F>(f), std::move(value())); else return std::remove_cvref_t<std::invoke_result_t<F, T>>{};
4) Äquivalent zu
if (*this) return std::invoke(std::forward<F>(f), std::move(value()); else return std::remove_cvref_t<std::invoke_result_t<F, const T>>{};
Inhalt |
[bearbeiten] Parameter
| f | - | eine geeignete Funktion oder ein Callable-Objekt, das ein std::optional zurückgibt |
[bearbeiten] Rückgabewert
Das Ergebnis von f oder ein leeres std::optional, wie oben beschrieben.
[bearbeiten] Hinweise
Einige Sprachen bezeichnen diese Operation als flatmap.
| Feature-Test-Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_optional |
202110L |
(C++23) | Monadische Operationen in std::optional |
[bearbeiten] Beispiel
Führen Sie diesen Code aus
#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; else return std::nullopt; } int main() { using namespace std::literals; const std::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([]{ return std::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) { return std::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'; }
Ausgabe
"1234" -> 1235 "15 foo" -> 16 "bar" -> NaN "42" -> 43 "5000000000" -> NaN " 5" -> NaN "nullopt" -> NaN "-43" -> -42
[bearbeiten] Siehe auch
| gibt den enthaltenen Wert zurück, falls vorhanden, sonst einen anderen Wert (public member function) | |
| (C++23) |
gibt ein optional zurück, das den transformierten enthaltenen Wert enthält, falls dieser existiert, andernfalls ein leeres optional(public member function) |
| (C++23) |
gibt das optional selbst zurück, wenn es einen Wert enthält, andernfalls das Ergebnis der gegebenen Funktion(public member function) |