Files
cppdraft_translate/cppdraft/expected/unexpected.md
2025-10-25 03:02:53 +03:00

7.4 KiB
Raw Blame History

[expected.unexpected]

22 General utilities library [utilities]

22.8 Expected objects [expected]

22.8.3 Class template unexpected [expected.unexpected]

22.8.3.1 General [expected.un.general]

1

#

Subclause [expected.unexpected] describes the class template unexpected that represents unexpected objects stored in expected objects.

🔗

namespace std {templateclass unexpected {public:// [expected.un.cons], constructorsconstexpr unexpected(const unexpected&) = default; constexpr unexpected(unexpected&&) = default; templateconstexpr 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, 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(see below); templatefriend constexpr bool operator==(const unexpected&, const unexpected&); friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y))); private: E unex; // exposition only}; template unexpected(E) -> unexpected;}

2

#

A program that instantiates the definition of unexpected for a non-object type, an array type, a specialization of unexpected, or a cv-qualified type is ill-formed.

22.8.3.2 Constructors [expected.un.cons]

🔗

template<class Err = E> constexpr explicit unexpected(Err&& e);

1

#

Constraints:

is_same_v<remove_cvref_t, unexpected> is false; and

is_same_v<remove_cvref_t, in_place_t> is false; and

is_constructible_v<E, Err> is true.

2

#

Effects: Direct-non-list-initializes unex with std::forward(e).

3

#

Throws: Any exception thrown by the initialization of unex.

🔗

template<class... Args> constexpr explicit unexpected(in_place_t, Args&&... args);

4

#

Constraints: is_constructible_v<E, Args...> is true.

5

#

Effects: Direct-non-list-initializesunex with std::forward(args)....

6

#

Throws: Any exception thrown by the initialization of unex.

🔗

template<class U, class... Args> constexpr explicit unexpected(in_place_t, initializer_list<U> il, Args&&... args);

7

#

Constraints: is_constructible_v<E, initializer_list&, Args...> is true.

8

#

Effects: Direct-non-list-initializesunex with il, std::forward(args)....

9

#

Throws: Any exception thrown by the initialization of unex.

22.8.3.3 Observers [expected.un.obs]

🔗

constexpr const E& error() const & noexcept; constexpr E& error() & noexcept;

1

#

Returns: unex.

🔗

constexpr E&& error() && noexcept; constexpr const E&& error() const && noexcept;

2

#

Returns: std::move(unex).

22.8.3.4 Swap [expected.un.swap]

🔗

constexpr void swap(unexpected& other) noexcept(is_nothrow_swappable_v<E>);

1

#

Mandates: is_swappable_v is true.

2

#

Effects: Equivalent to:using std::swap; swap(unex, other.unex);

🔗

friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y)));

3

#

Constraints: is_swappable_v is true.

4

#

Effects: Equivalent to x.swap(y).

22.8.3.5 Equality operator [expected.un.eq]

🔗

template<class E2> friend constexpr bool operator==(const unexpected& x, const unexpected<E2>& y);

1

#

Mandates: The expression x.error() == y.error() is well-formed and its result is convertible to bool.

2

#

Returns: x.error() == y.error().