[expected.unexpected]

# 22 General utilities library [[utilities]](./#utilities)

## 22.8 Expected objects [[expected]](expected#unexpected)

### 22.8.3 Class template unexpected [expected.unexpected]

#### [22.8.3.1](#expected.un.general) General [[expected.un.general]](expected.un.general)

[1](#expected.un.general-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7698)

Subclause [expected.unexpected] describes the class template unexpected that represents unexpected objects stored in expected objects[.](#expected.un.general-1.sentence-1)

[ð](#lib:unexpected)

namespace std {template<class E>class unexpected {public:// [[expected.un.cons]](#expected.un.cons "22.8.3.2 Constructors"), constructorsconstexpr 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(*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 *unex*; // *exposition only*}; template<class E> unexpected(E) -> unexpected<E>;}

[2](#expected.un.general-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7741)

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[.](#expected.un.general-2.sentence-1)

#### [22.8.3.2](#expected.un.cons) Constructors [[expected.un.cons]](expected.un.cons)

[ð](#lib:unexpected,constructor)

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

[1](#expected.un.cons-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7758)

*Constraints*: 

- [(1.1)](#expected.un.cons-1.1)

is_same_v<remove_cvref_t<Err>, unexpected> is false; and

- [(1.2)](#expected.un.cons-1.2)

is_same_v<remove_cvref_t<Err>, in_place_t> is false; and

- [(1.3)](#expected.un.cons-1.3)

is_constructible_v<E, Err> is true[.](#expected.un.cons-1.sentence-1)

[2](#expected.un.cons-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7769)

*Effects*: Direct-non-list-initializes *unex* with std​::​forward<Err>(e)[.](#expected.un.cons-2.sentence-1)

[3](#expected.un.cons-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7773)

*Throws*: Any exception thrown by the initialization of *unex*[.](#expected.un.cons-3.sentence-1)

[ð](#lib:unexpected,constructor_)

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

[4](#expected.un.cons-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7785)

*Constraints*: is_constructible_v<E, Args...> is true[.](#expected.un.cons-4.sentence-1)

[5](#expected.un.cons-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7789)

*Effects*: Direct-non-list-initializes*unex* with std​::​forward<Args>(args)...[.](#expected.un.cons-5.sentence-1)

[6](#expected.un.cons-6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7794)

*Throws*: Any exception thrown by the initialization of *unex*[.](#expected.un.cons-6.sentence-1)

[ð](#lib:unexpected,constructor__)

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

[7](#expected.un.cons-7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7806)

*Constraints*: is_constructible_v<E, initializer_list<U>&, Args...> is true[.](#expected.un.cons-7.sentence-1)

[8](#expected.un.cons-8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7810)

*Effects*: Direct-non-list-initializes*unex* with il, std​::​forward<Args>(args)...[.](#expected.un.cons-8.sentence-1)

[9](#expected.un.cons-9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7815)

*Throws*: Any exception thrown by the initialization of *unex*[.](#expected.un.cons-9.sentence-1)

#### [22.8.3.3](#expected.un.obs) Observers [[expected.un.obs]](expected.un.obs)

[ð](#lib:error,unexpected)

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

[1](#expected.un.obs-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7829)

*Returns*: *unex*[.](#expected.un.obs-1.sentence-1)

[ð](#lib:error,unexpected_)

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

[2](#expected.un.obs-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7841)

*Returns*: std​::​move(*unex*)[.](#expected.un.obs-2.sentence-1)

#### [22.8.3.4](#expected.un.swap) Swap [[expected.un.swap]](expected.un.swap)

[ð](#lib:swap,unexpected)

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

[1](#expected.un.swap-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7854)

*Mandates*: is_swappable_v<E> is true[.](#expected.un.swap-1.sentence-1)

[2](#expected.un.swap-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7858)

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

[ð](#expected.un.swap-itemdecl:2)

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

[3](#expected.un.swap-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7869)

*Constraints*: is_swappable_v<E> is true[.](#expected.un.swap-3.sentence-1)

[4](#expected.un.swap-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7873)

*Effects*: Equivalent to x.swap(y)[.](#expected.un.swap-4.sentence-1)

#### [22.8.3.5](#expected.un.eq) Equality operator [[expected.un.eq]](expected.un.eq)

[ð](#lib:operator==,unexpected)

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

[1](#expected.un.eq-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7887)

*Mandates*: The expression x.error() == y.error() is well-formed and
its result is convertible to bool[.](#expected.un.eq-1.sentence-1)

[2](#expected.un.eq-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7892)

*Returns*: x.error() == y.error()[.](#expected.un.eq-2.sentence-1)
