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

16 KiB
Raw Permalink Blame History

[optional.assign]

22 General utilities library [utilities]

22.5 Optional objects [optional]

22.5.3 Class template optional [optional.optional]

22.5.3.4 Assignment [optional.assign]

🔗

constexpr optional<T>& operator=(nullopt_t) noexcept;

1

#

Effects: If *this contains a value, calls val->T::~T() to destroy the contained value; otherwise no effect.

2

#

Postconditions: *this does not contain a value.

3

#

Returns: *this.

🔗

constexpr optional<T>& operator=(const optional& rhs);

4

#

Effects: See Table 67.

Table 67 — optional::operator=(const optional&) effects [tab:optional.assign.copy]

🔗 *this contains a value *this does not contain a value
🔗
rhs contains a value
assigns *rhs to the contained value direct-non-list-initializes the contained value with *rhs
🔗
rhs does not contain a value
destroys the contained value by calling val->T::~T() no effect

5

#

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

6

#

Returns: *this.

7

#

Remarks: If any exception is thrown, the result of the expression this->has_value() remains unchanged.

If an exception is thrown during the call to T's copy constructor, no effect.

If an exception is thrown during the call to T's copy assignment, the state of its contained value is as defined by the exception safety guarantee of T's copy assignment.

This operator is defined as deleted unlessis_copy_constructible_v is true andis_copy_assignable_v is true.

If is_trivially_copy_constructible_v &&is_trivially_copy_assignable_v &&is_trivially_destructible_v is true, this assignment operator is trivial.

🔗

constexpr optional& operator=(optional&& rhs) noexcept(see below);

8

#

Constraints: is_move_constructible_v is true andis_move_assignable_v is true.

9

#

Effects: See Table 68.

The result of the expression rhs.has_value() remains unchanged.

Table 68 — optional::operator=(optional&&) effects [tab:optional.assign.move]

🔗 *this contains a value *this does not contain a value
🔗
rhs contains a value
assigns *std::move(rhs) to the contained value direct-non-list-initializes the contained value with *std::move(rhs)
🔗
rhs does not contain a value
destroys the contained value by calling val->T::~T() no effect

10

#

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

11

#

Returns: *this.

12

#

Remarks: The exception specification is equivalent to:is_nothrow_move_assignable_v && is_nothrow_move_constructible_v

13

#

If any exception is thrown, the result of the expression this->has_value() remains unchanged.

If an exception is thrown during the call to T's move constructor, the state of *rhs.val is determined by the exception safety guarantee of T's move constructor.

If an exception is thrown during the call to T's move assignment, the state of *val and *rhs.val is determined by the exception safety guarantee of T's move assignment.

If is_trivially_move_constructible_v &&is_trivially_move_assignable_v &&is_trivially_destructible_v is true, this assignment operator is trivial.

🔗

template<class U = remove_cv_t<T>> constexpr optional& operator=(U&& v);

14

#

Constraints:

is_same_v<remove_cvref_t, optional> is false,

conjunction_v<is_scalar, is_same<T, decay_t>> is false,

is_constructible_v<T, U> is true, and

is_assignable_v<T&, U> is true.

15

#

Effects: If *this contains a value, assigns std::forward(v) to the contained value; otherwise direct-non-list-initializes the contained value with std::forward(v).

16

#

Postconditions: *this contains a value.

17

#

Returns: *this.

18

#

Remarks: If any exception is thrown, the result of the expression this->has_value() remains unchanged.

If an exception is thrown during the call to T's constructor, the state of v is determined by the exception safety guarantee of T's constructor.

If an exception is thrown during the call to T's assignment, the state of *val and v is determined by the exception safety guarantee of T's assignment.

🔗

template<class U> constexpr optional<T>& operator=(const optional<U>& rhs);

19

#

Constraints:

is_constructible_v<T, const U&> is true,

is_assignable_v<T&, const U&> is true,

converts-from-any-cvref<T, optional> is false,

is_assignable_v<T&, optional&> is false,

is_assignable_v<T&, optional&&> is false,

is_assignable_v<T&, const optional&> is false, and

is_assignable_v<T&, const optional&&> is false.

20

#

Effects: See Table 69.

Table 69 — optional::operator=(const optional&) effects [tab:optional.assign.copy.templ]

🔗 *this contains a value *this does not contain a value
🔗
rhs contains a value
assigns *rhs to the contained value direct-non-list-initializes the contained value with *rhs
🔗
rhs does not contain a value
destroys the contained value by calling val->T::~T() no effect

21

#

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

22

#

Returns: *this.

23

#

Remarks: If any exception is thrown, the result of the expression this->has_value() remains unchanged.

If an exception is thrown during the call to T's constructor, the state of *rhs.val is determined by the exception safety guarantee of T's constructor.

If an exception is thrown during the call to T's assignment, the state of *val and *rhs.val is determined by the exception safety guarantee of T's assignment.

🔗

template<class U> constexpr optional<T>& operator=(optional<U>&& rhs);

24

#

Constraints:

is_constructible_v<T, U> is true,

is_assignable_v<T&, U> is true,

converts-from-any-cvref<T, optional> is false,

is_assignable_v<T&, optional&> is false,

is_assignable_v<T&, optional&&> is false,

is_assignable_v<T&, const optional&> is false, and

is_assignable_v<T&, const optional&&> is false.

25

#

Effects: See Table 70.

The result of the expression rhs.has_value() remains unchanged.

Table 70 — optional::operator=(optional&&) effects [tab:optional.assign.move.templ]

🔗 *this contains a value *this does not contain a value
🔗
rhs contains a value
assigns *std::move(rhs) to the contained value direct-non-list-initializes the contained value with *std::move(rhs)
🔗
rhs does not contain a value
destroys the contained value by calling val->T::~T() no effect

26

#

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

27

#

Returns: *this.

28

#

Remarks: If any exception is thrown, the result of the expression this->has_value() remains unchanged.

If an exception is thrown during the call to T's constructor, the state of *rhs.val is determined by the exception safety guarantee of T's constructor.

If an exception is thrown during the call to T's assignment, the state of *val and *rhs.val is determined by the exception safety guarantee of T's assignment.

🔗

template<class... Args> constexpr T& emplace(Args&&... args);

29

#

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

30

#

Effects: Calls *this = nullopt.

Then direct-non-list-initializes the contained value with std::forward(args)....

31

#

Postconditions: *this contains a value.

32

#

Returns: A reference to the new contained value.

33

#

Throws: Any exception thrown by the selected constructor of T.

34

#

Remarks: If an exception is thrown during the call to T's constructor, *this does not contain a value, and the previous *val (if any) has been destroyed.

🔗

template<class U, class... Args> constexpr T& emplace(initializer_list<U> il, Args&&... args);

35

#

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

36

#

Effects: Calls *this = nullopt.

Then direct-non-list-initializes the contained value withil, std::forward(args)....

37

#

Postconditions: *this contains a value.

38

#

Returns: A reference to the new contained value.

39

#

Throws: Any exception thrown by the selected constructor of T.

40

#

Remarks: If an exception is thrown during the call to T's constructor, *this does not contain a value, and the previous *val (if any) has been destroyed.