variant& operator=(const variant& rhs) | (1) | (since C++17) |
variant& operator=(variant&& rhs) | (2) | (since C++17) |
template <class T> variant& operator=(T&& t) | (3) | (since C++17) |
Assigns a new value to an existing variant
object.
*this
and rhs
are valueless by exception, does nothing rhs
is valueless, but *this
is not, destroys the value contained in *this
and makes it valueless rhs
holds the same alternative as *this
, assigns the value contained in rhs
to the value contained in *this
. If an exception is thrown, *this
does not become valueless: the value depends on the exception safety guarantee of the alternative's copy assignment. rhs
and *this
hold different alternatives) rhs
to a temporary. If an exception is thrown by the copy constructor, *this
remains unchanged *this
*this
to hold the same alternative as rhs
*this
as if direct-non-list-initializing an object of type T_j
with std::forward<T_j>(TMP)
, with TMP
being the temporary and j
being rhs.index()
. If an exception is thrown by T_j's move constructor, *this
becomes valueless_by_exception
std::is_copy_constructible_v<T_i> && std::is_move_constructible_v<T_i> && std::is_copy_assignable_v<T_i>
is true
for all T_i
in Types...
*this
and rhs
are valueless by exception, does nothing rhs
is valueless, but *this
is not, destroys the value contained in *this
and makes it valueless rhs
holds the same alternative as *this
, assigns std::get<j>(std::move(rhs))
to the value contained in *this
, with j
being index()
. If an exception is thrown, *this
does not become valueless: the value depends on the exception safety guarantee of the alternative's move assignment. rhs
and *this
hold different alternatives) *this
*this
to hold the same alternative as rhs
*this
as if direct-non-list-initializing an object of type T_j
with std::get<j>(std::move(rhs))
with j
being rhs.index()
. If an exception is thrown by T_i
's move constructor, *this
becomes valueless_by_exception
std::is_move_constructible_v<T_i> && std::is_move_assignable_v<T_i>
is true
for all T_i
in Types...
. T_j
that would be selected by overload resolution for the expression F(std::forward<T>(t))
if there was an overload of imaginary function F(T_i)
for every T_i
from Types...
in scope at the same time. *this
already holds a T_j
, assigns std::forward<T>(t)
to the value contained in *this
. If an exception is thrown, *this
does not become valueless: the value depends on the exception safety guarantee of the assignment called. *this
(if any) this
to hold the alternative T_j
as selected above std::forward<T>(t)
as the initializer. If an exception is thrown during this initialization, *this
becomes valueless_by_exception
This overload only participates in overload resolution if std::is_same_v<std::decay_t<T>, variant>
is false and std::is_assignable_v<T_j&, T>
is true and std::is_constructible_v<T_j, T>
is true and the expression F(std::forward<T>(t))
(with F being the above-mentioned set of imaginary functions) is well formed.
variant<string> v1; v1 = "abc"; // OK variant<string, string> v2; v2 = "abc"; // Error variant <string, bool> v3; v3 = "abc"; // OK but chooses bool
rhs | - | another variant object |
t | - | a value convertible to one of the variant's alternatives |
*this
.
noexcept
specification: noexcept(((std::is_nothrow_move_constructible_v<Types> && is_nothrow_move_assignable_v<Types>) && ...))
noexcept
specification: noexcept(std::is_nothrow_assignable_v<T_j&, T> && std::is_nothrow_constructible_v<T_j, T>)
constructs a value in the variant, 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/variant/operator=