std::optional<T>::and_then
Von de.cppreference.net
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 (anders als transform()). Andernfalls ist das Programm fehlerhaft.
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>>{};
Parameter
| f | - | eine geeignete Funktion oder Callable Objekt, das einen std::optional zurückgibt |
Rückgabewert
Das Ergebnis von f oder ein leerer std::optional , wie oben beschrieben.
Hinweise
Einige Sprachen nennen diese Operation flatmap .
| Feature-Test Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_optional
|
202110L
|
(C++23) | Monadische Operationen in std::optional |
Beispiel
Diesen Code ausführen
#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) { // Inhalt des optional<string> Eingabeparameters debug-ausgeben std::cout << std::left << std::setw(13) << std::quoted(o.value_or("nullopt")) << " -> "; return o // falls optional nullopt ist, in optional mit ""-String umwandeln .or_else([]{ return std::optional{""s}; }) // flatmap von Strings zu Ganzzahlen (erzeugt leere optionals bei Fehlschlag) .and_then(to_int) // Ganzzahl auf Ganzzahl + 1 abbilden .transform([](int n) { return n + 1; }) // zurück zu Strings konvertieren .transform([](int n) { return std::to_string(n); }) // alle leeren optionals, die von and_then übrig blieben // und von transforms ignoriert wurden, durch "NaN" ersetzen .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
Siehe auch
|
gibt den enthaltenen Wert zurück, falls verfügbar, andernfalls einen anderen Wert
(öffentliche Elementfunktion) |
|
|
(C++23)
|
gibt ein
optional
zurück, das den transformierten enthaltenen Wert enthält, falls vorhanden, oder ein leeres
optional
andernfalls
(öffentliche Elementfunktion) |
|
(C++23)
|
gibt das
optional
selbst zurück, falls es einen Wert enthält, oder das Ergebnis der gegebenen Funktion andernfalls
(öffentliche Elementfunktion) |