std::optional<T>::swap
void swap( optional& other ) noexcept(/* see below */); | (since C++17) |
Swaps the contents with those of other
.
- If neither
*this
norother
contain a value, the function has no effect. - If only one of
*this
andother
contains a value (let's call this objectin
and the otherun
), the contained value ofun
is direct-initialized fromstd::move(*in)
, followed by destruction of the contained value ofin
as if byin->T::~T()
. After this call,in
does not contain a value;un
contains a value. - If both
*this
andother
contain values, the contained values are exchanged by callingusing std::swap; swap(**this, *other)
.T
lvalues must satisfy Swappable.
Parameters
other | - | the optional object to exchange the contents with |
Return value
(none).
Exceptions
noexcept
specification: noexcept(std::is_nothrow_move_constructible_v<T> &&
std::is_nothrow_swappable_v<T>)
In the case of thrown exception, the states of the contained values of *this
and other
are determined by the exception safety guarantees of swap
of type T
or T
's move constructor, whichever is called. For both *this
and other
, if the object contained a value, it is left containing a value, and the other way round.
See also
(C++17) | specializes the std::swap algorithm (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/utility/optional/swap