Files
cppdraft_translate/cppdraft/optional/swap.md
2025-10-25 03:02:53 +03:00

3.1 KiB
Raw Blame History

[optional.swap]

22 General utilities library [utilities]

22.5 Optional objects [optional]

22.5.3 Class template optional [optional.optional]

22.5.3.5 Swap [optional.swap]

🔗

constexpr void swap(optional& rhs) noexcept(see below);

1

#

Mandates: is_move_constructible_v is true.

2

#

Preconditions: T meets the Cpp17Swappable requirements ([swappable.requirements]).

3

#

Effects: See Table 71.

Table 71 — optional::swap(optional&) effects [tab:optional.swap]

🔗 *this contains a value *this does not contain a value
🔗
rhs contains a value
calls swap(*(*this), *rhs) direct-non-list-initializes the contained value of *this with std::move(*rhs), followed by rhs.val->T::~T(); postcondition is that *this contains a value and rhs does not contain a value
🔗
rhs does not contain a value
direct-non-list-initializes the contained value of rhs with std::move(*(*this)), followed by val->T::~T(); postcondition is that *this does not contain a value and rhs contains a value no effect

4

#

Throws: Any exceptions thrown by the operations in the relevant part of Table 71.

5

#

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

6

#

If any exception is thrown, the results of the expressions this->has_value() and rhs.has_value() remain unchanged.

If an exception is thrown during the call to function swap, the state of *val and *rhs.val is determined by the exception safety guarantee of swap for lvalues of T.

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