[expected.object.cons]

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

## 22.8 Expected objects [[expected]](expected#object.cons)

### 22.8.6 Class template expected [[expected.expected]](expected.expected#expected.object.cons)

#### 22.8.6.2 Constructors [expected.object.cons]

[1](#1)

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

The exposition-only variable template *converts-from-any-cvref* defined in [[optional.ctor]](optional.ctor "22.5.3.2 Constructors") is used by some constructors for expected[.](#1.sentence-1)

[ð](#lib:expected,constructor)

`constexpr expected();
`

[2](#2)

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

*Constraints*: is_default_constructible_v<T> is true[.](#2.sentence-1)

[3](#3)

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

*Effects*: Value-initializes *val*[.](#3.sentence-1)

[4](#4)

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

*Postconditions*: has_value() is true[.](#4.sentence-1)

[5](#5)

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

*Throws*: Any exception thrown by the initialization of *val*[.](#5.sentence-1)

[ð](#lib:expected,constructor_)

`constexpr expected(const expected& rhs);
`

[6](#6)

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

*Effects*: If rhs.has_value() is true,
direct-non-list-initializes *val* with *rhs[.](#6.sentence-1)

Otherwise, direct-non-list-initializes *unex* with rhs.error()[.](#6.sentence-2)

[7](#7)

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

*Postconditions*: rhs.has_value() == this->has_value()[.](#7.sentence-1)

[8](#8)

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

*Throws*: Any exception thrown by the initialization of *val* or *unex*[.](#8.sentence-1)

[9](#9)

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

*Remarks*: This constructor is defined as deleted unless

- [(9.1)](#9.1)

is_copy_constructible_v<T> is true and

- [(9.2)](#9.2)

is_copy_constructible_v<E> is true[.](#9.sentence-1)

[10](#10)

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

This constructor is trivial if

- [(10.1)](#10.1)

is_trivially_copy_constructible_v<T> is true and

- [(10.2)](#10.2)

is_trivially_copy_constructible_v<E> is true[.](#10.sentence-1)

[ð](#lib:expected,constructor__)

`constexpr expected(expected&& rhs) noexcept(see below);
`

[11](#11)

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

*Constraints*: 

- [(11.1)](#11.1)

is_move_constructible_v<T> is true and

- [(11.2)](#11.2)

is_move_constructible_v<E> is true[.](#11.sentence-1)

[12](#12)

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

*Effects*: If rhs.has_value() is true,
direct-non-list-initializes *val* with std​::​move(*rhs)[.](#12.sentence-1)

Otherwise,
direct-non-list-initializes *unex* with std​::​move(rhs.error())[.](#12.sentence-2)

[13](#13)

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

*Postconditions*: rhs.has_value() is unchanged;rhs.has_value() == this->has_value() is true[.](#13.sentence-1)

[14](#14)

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

*Throws*: Any exception thrown by the initialization of *val* or *unex*[.](#14.sentence-1)

[15](#15)

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

*Remarks*: The exception specification is equivalent tois_nothrow_move_constructible_v<T> && is_nothrow_move_constructible_v<E>[.](#15.sentence-1)

[16](#16)

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

This constructor is trivial if

- [(16.1)](#16.1)

is_trivially_move_constructible_v<T> is true and

- [(16.2)](#16.2)

is_trivially_move_constructible_v<E> is true[.](#16.sentence-1)

[ð](#lib:expected,constructor___)

`template<class U, class G>
  constexpr explicit(see below) expected(const expected<U, G>& rhs);
template<class U, class G>
  constexpr explicit(see below) expected(expected<U, G>&& rhs);
`

[17](#17)

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

Let:

- [(17.1)](#17.1)

  UF be const U& for the first overload andU for the second overload[.](#17.1.sentence-1)

- [(17.2)](#17.2)

  GF be const G& for the first overload andG for the second overload[.](#17.2.sentence-1)

[18](#18)

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

*Constraints*: 

- [(18.1)](#18.1)

is_constructible_v<T, UF> is true; and

- [(18.2)](#18.2)

is_constructible_v<E, GF> is true; and

- [(18.3)](#18.3)

if T is not cv bool,*converts-from-any-cvref*<T, expected<U, G>> is false; and

- [(18.4)](#18.4)

is_constructible_v<unexpected<E>, expected<U, G>&> is false; and

- [(18.5)](#18.5)

is_constructible_v<unexpected<E>, expected<U, G>> is false; and

- [(18.6)](#18.6)

is_constructible_v<unexpected<E>, const expected<U, G>&> is false; and

- [(18.7)](#18.7)

is_constructible_v<unexpected<E>, const expected<U, G>> is false[.](#18.sentence-1)

[19](#19)

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

*Effects*: If rhs.has_value(),
direct-non-list-initializes *val* with std​::​forward<UF>(*rhs)[.](#19.sentence-1)

Otherwise,
direct-non-list-initializes *unex* with std​::​forward<GF>(rhs.error())[.](#19.sentence-2)

[20](#20)

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

*Postconditions*: rhs.has_value() is unchanged;rhs.has_value() == this->has_value() is true[.](#20.sentence-1)

[21](#21)

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

*Throws*: Any exception thrown by the initialization of *val* or *unex*[.](#21.sentence-1)

[22](#22)

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

*Remarks*: The expression inside explicit is equivalent to!is_convertible_v<UF, T> || !is_convertible_v<GF, E>[.](#22.sentence-1)

[ð](#lib:expected,constructor____)

`template<class U = remove_cv_t<T>>
  constexpr explicit(!is_convertible_v<U, T>) expected(U&& v);
`

[23](#23)

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

*Constraints*: 

- [(23.1)](#23.1)

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

- [(23.2)](#23.2)

is_same_v<remove_cvref_t<U>, expected> is false; and

- [(23.3)](#23.3)

is_same_v<remove_cvref_t<U>, unexpect_t> is false; and

- [(23.4)](#23.4)

remove_cvref_t<U> is not a specialization of unexpected; and

- [(23.5)](#23.5)

is_constructible_v<T, U> is true; and

- [(23.6)](#23.6)

if T is cv bool,remove_cvref_t<U> is not a specialization of expected[.](#23.sentence-1)

[24](#24)

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

*Effects*: Direct-non-list-initializes *val* with std​::​forward<U>(v)[.](#24.sentence-1)

[25](#25)

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

*Postconditions*: has_value() is true[.](#25.sentence-1)

[26](#26)

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

*Throws*: Any exception thrown by the initialization of *val*[.](#26.sentence-1)

[ð](#lib:expected,constructor_____)

`template<class G>
  constexpr explicit(!is_convertible_v<const G&, E>) expected(const unexpected<G>& e);
template<class G>
  constexpr explicit(!is_convertible_v<G, E>) expected(unexpected<G>&& e);
`

[27](#27)

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

Let GF be const G& for the first overload andG for the second overload[.](#27.sentence-1)

[28](#28)

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

*Constraints*: is_constructible_v<E, GF> is true[.](#28.sentence-1)

[29](#29)

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

*Effects*: Direct-non-list-initializes *unex* with std​::​forward<GF>(e.error())[.](#29.sentence-1)

[30](#30)

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

*Postconditions*: has_value() is false[.](#30.sentence-1)

[31](#31)

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

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

[ð](#lib:expected,constructor______)

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

[32](#32)

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

*Constraints*: is_constructible_v<T, Args...> is true[.](#32.sentence-1)

[33](#33)

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

*Effects*: Direct-non-list-initializes *val* with std​::​forward<Args>(args)...[.](#33.sentence-1)

[34](#34)

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

*Postconditions*: has_value() is true[.](#34.sentence-1)

[35](#35)

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

*Throws*: Any exception thrown by the initialization of *val*[.](#35.sentence-1)

[ð](#lib:expected,constructor_______)

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

[36](#36)

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

*Constraints*: is_constructible_v<T, initializer_list<U>&, Args...> is true[.](#36.sentence-1)

[37](#37)

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

*Effects*: Direct-non-list-initializes *val* withil, std​::​forward<Args>(args)...[.](#37.sentence-1)

[38](#38)

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

*Postconditions*: has_value() is true[.](#38.sentence-1)

[39](#39)

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

*Throws*: Any exception thrown by the initialization of *val*[.](#39.sentence-1)

[ð](#lib:expected,constructor________)

`template<class... Args>
  constexpr explicit expected(unexpect_t, Args&&... args);
`

[40](#40)

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

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

[41](#41)

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

*Effects*: Direct-non-list-initializes *unex* withstd​::​forward<Args>(args)...[.](#41.sentence-1)

[42](#42)

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

*Postconditions*: has_value() is false[.](#42.sentence-1)

[43](#43)

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

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

[ð](#lib:expected,constructor_________)

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

[44](#44)

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

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

[45](#45)

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

*Effects*: Direct-non-list-initializes *unex* withil, std​::​forward<Args>(args)...[.](#45.sentence-1)

[46](#46)

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

*Postconditions*: has_value() is false[.](#46.sentence-1)

[47](#47)

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

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