Files
2025-10-25 03:02:53 +03:00

13 KiB
Raw Permalink Blame History

[expected.object.cons]

22 General utilities library [utilities]

22.8 Expected objects [expected]

22.8.6 Class template expected [expected.expected]

22.8.6.2 Constructors [expected.object.cons]

1

#

The exposition-only variable template converts-from-any-cvref defined in [optional.ctor] is used by some constructors for expected.

🔗

constexpr expected();

2

#

Constraints: is_default_constructible_v is true.

3

#

Effects: Value-initializes val.

4

#

Postconditions: has_value() is true.

5

#

Throws: Any exception thrown by the initialization of val.

🔗

constexpr expected(const expected& rhs);

6

#

Effects: If rhs.has_value() is true, direct-non-list-initializes val with *rhs.

Otherwise, direct-non-list-initializes unex with rhs.error().

7

#

Postconditions: rhs.has_value() == this->has_value().

8

#

Throws: Any exception thrown by the initialization of val or unex.

9

#

Remarks: This constructor is defined as deleted unless

is_copy_constructible_v is true and

is_copy_constructible_v is true.

10

#

This constructor is trivial if

is_trivially_copy_constructible_v is true and

is_trivially_copy_constructible_v is true.

🔗

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

11

#

Constraints:

is_move_constructible_v is true and

is_move_constructible_v is true.

12

#

Effects: If rhs.has_value() is true, direct-non-list-initializes val with std::move(*rhs).

Otherwise, direct-non-list-initializes unex with std::move(rhs.error()).

13

#

Postconditions: rhs.has_value() is unchanged;rhs.has_value() == this->has_value() is true.

14

#

Throws: Any exception thrown by the initialization of val or unex.

15

#

Remarks: The exception specification is equivalent tois_nothrow_move_constructible_v && is_nothrow_move_constructible_v.

16

#

This constructor is trivial if

is_trivially_move_constructible_v is true and

is_trivially_move_constructible_v is true.

🔗

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

#

Let:

  • (17.1)

    UF be const U& for the first overload andU for the second overload.

  • (17.2)

    GF be const G& for the first overload andG for the second overload.

18

#

Constraints:

is_constructible_v<T, UF> is true; and

is_constructible_v<E, GF> is true; and

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

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

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

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

is_constructible_v<unexpected, const expected<U, G>> is false.

19

#

Effects: If rhs.has_value(), direct-non-list-initializes val with std::forward(*rhs).

Otherwise, direct-non-list-initializes unex with std::forward(rhs.error()).

20

#

Postconditions: rhs.has_value() is unchanged;rhs.has_value() == this->has_value() is true.

21

#

Throws: Any exception thrown by the initialization of val or unex.

22

#

Remarks: The expression inside explicit is equivalent to!is_convertible_v<UF, T> || !is_convertible_v<GF, E>.

🔗

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

23

#

Constraints:

is_same_v<remove_cvref_t, in_place_t> is false; and

is_same_v<remove_cvref_t, expected> is false; and

is_same_v<remove_cvref_t, unexpect_t> is false; and

remove_cvref_t is not a specialization of unexpected; and

is_constructible_v<T, U> is true; and

if T is cv bool,remove_cvref_t is not a specialization of expected.

24

#

Effects: Direct-non-list-initializes val with std::forward(v).

25

#

Postconditions: has_value() is true.

26

#

Throws: Any exception thrown by the initialization of val.

🔗

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

#

Let GF be const G& for the first overload andG for the second overload.

28

#

Constraints: is_constructible_v<E, GF> is true.

29

#

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

30

#

Postconditions: has_value() is false.

31

#

Throws: Any exception thrown by the initialization of unex.

🔗

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

32

#

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

33

#

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

34

#

Postconditions: has_value() is true.

35

#

Throws: Any exception thrown by the initialization of val.

🔗

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

36

#

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

37

#

Effects: Direct-non-list-initializes val withil, std::forward(args)....

38

#

Postconditions: has_value() is true.

39

#

Throws: Any exception thrown by the initialization of val.

🔗

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

40

#

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

41

#

Effects: Direct-non-list-initializes unex withstd::forward(args)....

42

#

Postconditions: has_value() is false.

43

#

Throws: Any exception thrown by the initialization of unex.

🔗

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

44

#

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

45

#

Effects: Direct-non-list-initializes unex withil, std::forward(args)....

46

#

Postconditions: has_value() is false.

47

#

Throws: Any exception thrown by the initialization of unex.