optional& operator=( std::nullopt_t ) noexcept; | (1) | (since C++17) |
optional& operator=( const optional& other ); | (2) | (since C++17) |
optional& operator=( optional&& other ) noexcept(/* see below */); | (3) | (since C++17) |
template< class U = T > optional& operator=( U&& value ); | (4) | (since C++17) |
template< class U > optional& operator=( const optional<U>& other ); | (5) | (since C++17) |
template< class U > optional& operator=( optional<U>&& other ); | (6) | (since C++17) |
Replaces contents of *this
with the contents of other
.
*this
contains a value before the call, the contained value is destroyed by calling its destructor as if by value().T::~T()
. *this
does not contain a value after this call.other
. *this
and other
do not contain a value, the function has no effect. *this
contains a value, but other
does not, then the contained value is destroyed by calling its destructor. *this
does not contain a value after the call. other
contains a value, then depending on whether *this
contains a value, the contained value is either direct-initialized or assigned from *other
(2) or std::move(*other)
(3). Note that a moved-from optional still contains a value. std::is_copy_constructible_v<T>
and std::is_copy_assignable_v<T>
are both true
. std::is_move_constructible_v<T>
and std::is_move_assignable_v<T>
are both true
.*this
contains a value before the call, the contained value is either direct-initialized from std::forward<U>(value)
or assigned from std::forward<U>(value)
. The function does not participate in overload resolution unless std::decay_t<U>
(until C++20)std::remove_cvref_t<U>
(since C++20) is not std::optional<T>
, std::is_constructible_v<T, U>
is true
, std::is_assignable_v<T&, U>
is true
, and at least one of the following is true: T
is not a scalar type; std::decay_t<U>
is not T
.other
. *this
and other
do not contain a value, the function has no effect. *this
contains a value, but other
does not, then the contained value is destroyed by calling its destructor. *this
does not contain a value after the call. other
contains a value, then depending on whether *this
contains a value, the contained value is either direct-initialized or assigned from *other
(5) or std::move(*other)
(6). Note that a moved-from optional still contains a value. T
is not constructible, convertible, or assignable from any expression of type (possibly const
) std::optional<U>
, i.e., the following 12 type traits are all false
: std::is_constructible_v<T, std::optional<U>&>
std::is_constructible_v<T, const std::optional<U>&>
std::is_constructible_v<T, std::optional<U>&&>
std::is_constructible_v<T, const std::optional<U>&&>
std::is_convertible_v<std::optional<U>&, T>
std::is_convertible_v<const std::optional<U>&, T>
std::is_convertible_v<std::optional<U>&&, T>
std::is_convertible_v<const std::optional<U>&&, T>
std::is_assignable_v<T&, std::optional<U>&>
std::is_assignable_v<T&, const std::optional<U>&>
std::is_assignable_v<T&, std::optional<U>&&>
std::is_assignable_v<T&, const std::optional<U>&&>
. std::is_constructible_v<T, const U&>
and std::is_assignable_v<T&, const U&>
are both true
. std::is_constructible_v<T, U>
and std::is_assignable_v<T&, U>
are both true
.other | - | another optional object whose contained value to assign |
value | - | value to assign to the contained value |
*this
.
T
. If an exception is thrown, the initialization state of *this
(and of other
in case of (2-3) and (5-6) ) is unchanged, i.e. if the object contained a value, it still contains a value, and the other way round. The contents of value
and the contained values of *this
and other
depend on the exception safety guarantees of the operation from which the exception originates (copy-constructor, move-assignment, etc.). noexcept
specification: noexcept(std::is_nothrow_move_assignable<T>::value && std::is_nothrow_move_constructible<T>::value)
An optional object op
may be turned into an empty optional with both op = {};
and op = nullopt;
. The first expression constructs an empty optional
object with {}
and assigns it to op
.
#include <optional> #include <iostream> int main() { std::optional<const char*> s1 = "abc", s2; // constructor s2 = s1; // assignment s1 = "def"; // decaying assignment (U = char[4], T = const char*) std::cout << *s2 << ' ' << *s1 << '\n'; }
Output:
abc def
constructs the contained value in-place (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/utility/optional/operator=