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]

Subclause [expected.unexpected] describes the class template unexpected that represents unexpected objects stored in expected objects.
namespace std { template<class E> class unexpected { public: 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& value() const & noexcept; constexpr E& value() & noexcept; constexpr const E&& value() const && noexcept; constexpr E&& value() && noexcept; constexpr void swap(unexpected& other) noexcept(see below); 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 val; // exposition only }; template<class E> unexpected(E) -> unexpected<E>; }
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.ctor]

template<class Err = E> constexpr explicit unexpected(Err&& e);
Constraints:
  • is_­same_­v<remove_­cvref_­t<Err>, unexpected> is false; and
  • is_­same_­v<remove_­cvref_­t<Err>, in_­place_­t> is false; and
  • is_­constructible_­v<E, Err> is true.
Effects: Direct-non-list-initializes val with std​::​forward<Err>(e).
Throws: Any exception thrown by the initialization of val.
template<class... Args> constexpr explicit unexpected(in_place_t, Args&&... args);
Constraints: is_­constructible_­v<E, Args...> is true.
Effects: Direct-non-list-initializes val with std​::​forward<Args>(args)....
Throws: Any exception thrown by the initialization of val.
template<class U, class... Args> constexpr explicit unexpected(in_place_t, initializer_list<U> il, Args&&... args);
Constraints: is_­constructible_­v<E, initializer_­list<U>&, Args...> is true.
Effects: Direct-non-list-initializes val with il, std​::​forward<Args>(args)....
Throws: Any exception thrown by the initialization of val.

22.8.3.3 Observers [expected.un.obs]

constexpr const E& value() const & noexcept; constexpr E& value() & noexcept;
Returns: val.
constexpr E&& value() && noexcept; constexpr const E&& value() const && noexcept;
Returns: std​::​move(val).

22.8.3.4 Swap [expected.un.swap]

constexpr void swap(unexpected& other) noexcept(is_nothrow_swappable_v<E>);
Mandates: is_­swappable_­v<E> is true.
Effects: Equivalent to: using std​::​swap; swap(val, other.val);
friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y)));
Constraints: is_­swappable_­v<E> is true.
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);
Mandates: The expression x.value() == y.value() is well-formed and its result is convertible to bool.
Returns: x.value() == y.value().