[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 {templateclass unexpected {public:// [[expected.un.cons]](#expected.un.cons "22.8.3.2 Constructors"), constructorsconstexpr unexpected(const unexpected&) = default; constexpr unexpected(unexpected&&) = default; templateconstexpr explicit unexpected(Err&&); templateconstexpr explicit unexpected(in_place_t, Args&&...); templateconstexpr 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](#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 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, unexpected> is false; and - [(1.2)](#expected.un.cons-1.2) is_same_v, in_place_t> is false; and - [(1.3)](#expected.un.cons-1.3) is_constructible_v 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(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 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 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)...[.](#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 constexpr explicit unexpected(in_place_t, initializer_list il, Args&&... args); ` [7](#expected.un.cons-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7806) *Constraints*: is_constructible_v&, 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)...[.](#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); ` [1](#expected.un.swap-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L7854) *Mandates*: is_swappable_v 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 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 friend constexpr bool operator==(const unexpected& x, const unexpected& 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)