Standard library header <expected> (C++23)
From cppreference.net
Dieser Header ist Teil der allgemeinen Utility-Bibliothek .
Klassen |
||
|
(C++23)
|
Ein Wrapper, der entweder einen erwarteten oder einen Fehlerwert enthält
(Klassentemplate) |
|
|
(C++23)
|
dargestellt als unerwarteter Wert
(Klassentemplate) |
|
|
(C++23)
|
Exception, die einen geprüften Zugriff auf einen
expected
anzeigt, der einen unerwarteten Wert enthält
(Klassentemplate) |
|
|
(C++23)
|
In-place-Konstruktions-Tag für unerwartete Werte in
expected
(Tag) |
|
Übersicht
// meistens freestanding namespace std { // Klassen-Template unexpected template<class E> class unexpected; // Klassen-Template bad_expected_access template<class E> class bad_expected_access; // Spezialisierung für void template<> class bad_expected_access<void>; // In-place-Konstruktion von unexpected-Werten struct unexpect_t { explicit unexpect_t() = default; }; inline constexpr unexpect_t unexpect{}; // Klassen-Template expected template<class T, class E> class expected; // teilweise freestanding // Partielle Spezialisierung von expected für void-Typen template<class T, class E> requires is_void_v<T> class expected<T, E>; // teilweise freestanding }
Klassentemplate std::unexpected
namespace std { template<class E> class unexpected { public: // Konstruktoren constexpr unexpected(const unexpected&) = default; constexpr unexpected(unexpected&&) = default; template<class Err = E> constexpr explicit unexpected(Err&&); template<class... Args> constexpr explicit unexpected(in_place_t, Args&&...); template<class U, class... Args> constexpr explicit unexpected(in_place_t, initializer_list<U>, Args&&...); constexpr unexpected& operator=(const unexpected&) = default; constexpr unexpected& operator=(unexpected&&) = default; constexpr const E& error() const& noexcept; constexpr E& error() & noexcept; constexpr const E&& error() const&& noexcept; constexpr E&& error() && noexcept; constexpr void swap(unexpected& other) noexcept(/* siehe Beschreibung */); template<class E2> friend constexpr bool operator==(const unexpected&, const unexpected<E2>&); friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y))); private: E /*unex*/; // nur zur Darstellung }; template<class E> unexpected(E) -> unexpected<E>; }
Klassentemplate std::bad_expected_access
namespace std { template<class E> class bad_expected_access : public bad_expected_access<void> { public: constexpr explicit bad_expected_access(E); constexpr const char* what() const noexcept override; constexpr E& error() & noexcept; constexpr const E& error() const& noexcept; constexpr E&& error() && noexcept; constexpr const E&& error() const&& noexcept; private: E /*unex*/; // nur zur Darstellung }; }
Klassentemplatespezialisierung std::bad_expected_access<void>
namespace std { template<> class bad_expected_access<void> : public exception { protected: constexpr bad_expected_access() noexcept; constexpr bad_expected_access(const bad_expected_access&) noexcept; constexpr bad_expected_access(bad_expected_access&&) noexcept; constexpr bad_expected_access& operator=(const bad_expected_access&) noexcept; constexpr bad_expected_access& operator=(bad_expected_access&&) noexcept; constexpr ~bad_expected_access(); public: constexpr const char* what() const noexcept override; }; }
Klassentemplate std::expected
namespace std { template<class T, class E> class expected { public: using value_type = T; using error_type = E; using unexpected_type = unexpected<E>; template<class U> using rebind = expected<U, error_type>; // Konstruktoren constexpr expected(); constexpr expected(const expected&); constexpr expected(expected&&) noexcept(/* siehe Beschreibung */); template<class U, class G> constexpr explicit(/* siehe Beschreibung */) expected(const expected<U, G>&); template<class U, class G> constexpr explicit(/* siehe Beschreibung */) expected(expected<U, G>&&); template<class U = remove_cv_t<T>> constexpr explicit(/* siehe Beschreibung */) expected(U&& v); template<class G> constexpr explicit(/* siehe Beschreibung */) expected(const unexpected<G>&); template<class G> constexpr explicit(/* siehe Beschreibung */) expected(unexpected<G>&&); template<class... Args> constexpr explicit expected(in_place_t, Args&&...); template<class U, class... Args> constexpr explicit expected(in_place_t, initializer_list<U>, Args&&...); template<class... Args> constexpr explicit expected(unexpect_t, Args&&...); template<class U, class... Args> constexpr explicit expected(unexpect_t, initializer_list<U>, Args&&...); // Destruktor constexpr ~expected(); // Zuweisung constexpr expected& operator=(const expected&); constexpr expected& operator=(expected&&) noexcept(/* siehe Beschreibung */); template<class U = remove_cv_t<T>> constexpr expected& operator=(U&&); template<class G> constexpr expected& operator=(const unexpected<G>&); template<class G> constexpr expected& operator=(unexpected<G>&&); template<class... Args> constexpr T& emplace(Args&&...) noexcept; template<class U, class... Args> constexpr T& emplace(initializer_list<U>, Args&&...) noexcept; // swap constexpr void swap(expected&) noexcept(/* siehe Beschreibung */); friend constexpr void swap(expected& x, expected& y) noexcept(noexcept(x.swap(y))); // Beobachter constexpr const T* operator->() const noexcept; constexpr T* operator->() noexcept; constexpr const T& operator*() const& noexcept; constexpr T& operator*() & noexcept; constexpr const T&& operator*() const&& noexcept; constexpr T&& operator*() && noexcept; constexpr explicit operator bool() const noexcept; constexpr bool has_value() const noexcept; constexpr const T& value() const&; // freestanding-deleted constexpr T& value() &; // freestanding-deleted constexpr const T&& value() const&&; // freestanding-deleted constexpr T&& value() &&; // freestanding-deleted constexpr const E& error() const& noexcept; constexpr E& error() & noexcept; constexpr const E&& error() const&& noexcept; constexpr E&& error() && noexcept; template<class U = remove_cv_t<T>> constexpr T value_or(U&&) const&; template<class U = remove_cv_t<T>> constexpr T value_or(U&&) &&; template<class G = E> constexpr E error_or(G&&) const&; template<class G = E> constexpr E error_or(G&&) &&; // monadische Operationen template<class F> constexpr auto and_then(F&& f) &; template<class F> constexpr auto and_then(F&& f) &&; template<class F> constexpr auto and_then(F&& f) const&; template<class F> constexpr auto and_then(F&& f) const&&; template<class F> constexpr auto or_else(F&& f) &; template<class F> constexpr auto or_else(F&& f) &&; template<class F> constexpr auto or_else(F&& f) const&; template<class F> constexpr auto or_else(F&& f) const&&; template<class F> constexpr auto transform(F&& f) &; template<class F> constexpr auto transform(F&& f) &&; template<class F> constexpr auto transform(F&& f) const&; template<class F> constexpr auto transform(F&& f) const&&; template<class F> constexpr auto transform_error(F&& f) &; template<class F> constexpr auto transform_error(F&& f) &&; template<class F> constexpr auto transform_error(F&& f) const&; template<class F> constexpr auto transform_error(F&& f) const&&; // Gleichheitsoperatoren template<class T2, class E2> requires(!is_void_v<T2>) friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y); template<class T2> friend constexpr bool operator==(const expected&, const T2&); template<class E2> friend constexpr bool operator==(const expected&, const unexpected<E2>&); private: bool /*has-val*/; // exposition-only union { T /*Wert*/; // exposition-only E /*unex*/; // exposition-only }; }; }
Partielle Spezialisierung von std::expected für void -Typen
template<class T, class E> requires is_void_v<T> class expected<T, E> { public: using value_type = T; using error_type = E; using unexpected_type = unexpected<E>; template<class U> using rebind = expected<U, error_type>; // Konstruktoren constexpr expected() noexcept; constexpr expected(const expected&); constexpr expected(expected&&) noexcept(/* siehe Beschreibung */); template<class U, class G> constexpr explicit(/* siehe Beschreibung */) expected(const expected<U, G>&); template<class U, class G> constexpr explicit(/* siehe Beschreibung */) expected(expected<U, G>&&); template<class G> constexpr explicit(/* siehe Beschreibung */) expected(const unexpected<G>&); template<class G> constexpr explicit(/* siehe Beschreibung */) expected(unexpected<G>&&); constexpr explicit expected(in_place_t) noexcept; template<class... Args> constexpr explicit expected(unexpect_t, Args&&...); template<class U, class... Args> constexpr explicit expected(unexpect_t, initializer_list<U>, Args&&...); // Destruktor constexpr ~expected(); // Zuweisung constexpr expected& operator=(const expected&); constexpr expected& operator=(expected&&) noexcept(/* siehe Beschreibung */); template<class G> constexpr expected& operator=(const unexpected<G>&); template<class G> constexpr expected& operator=(unexpected<G>&&); constexpr void emplace() noexcept; // swap constexpr void swap(expected&) noexcept(/* siehe Beschreibung */); friend constexpr void swap(expected& x, expected& y) noexcept(noexcept(x.swap(y))); // Beobachter constexpr explicit operator bool() const noexcept; constexpr bool has_value() const noexcept; constexpr void operator*() const noexcept; constexpr void value() const&; // freestanding-deleted constexpr void value() &&; // freestanding-deleted constexpr const E& error() const& noexcept; constexpr E& error() & noexcept; constexpr const E&& error() const&& noexcept; constexpr E&& error() && noexcept; template<class G = E> constexpr E error_or(G&&) const&; template<class G = E> constexpr E error_or(G&&) &&; // monadische Operationen template<class F> constexpr auto and_then(F&& f) &; template<class F> constexpr auto and_then(F&& f) &&; template<class F> constexpr auto and_then(F&& f) const&; template<class F> constexpr auto and_then(F&& f) const&&; template<class F> constexpr auto or_else(F&& f) &; template<class F> constexpr auto or_else(F&& f) &&; template<class F> constexpr auto or_else(F&& f) const&; template<class F> constexpr auto or_else(F&& f) const&&; template<class F> constexpr auto transform(F&& f) &; template<class F> constexpr auto transform(F&& f) &&; template<class F> constexpr auto transform(F&& f) const&; template<class F> constexpr auto transform(F&& f) const&&; template<class F> constexpr auto transform_error(F&& f) &; template<class F> constexpr auto transform_error(F&& f) &&; template<class F> constexpr auto transform_error(F&& f) const&; template<class F> constexpr auto transform_error(F&& f) const&&; // Gleichheitsoperatoren template<class T2, class E2> requires is_void_v<T2> friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y); template<class E2> friend constexpr bool operator==(const expected&, const unexpected<E2>&); private: bool /*has-val*/; // exposition-only union { E /*unex*/; // exposition-only }; };