std::variant::operator=
From cppreference.com
variant& operator=(const variant& rhs); |
(1) | (since C++17) |
variant& operator=(variant&& rhs) noexcept(/* see below */); |
(2) | (since C++17) |
template <class T> variant& operator=(T&& t) noexcept(/* see below */); |
(3) | (since C++17) |
Assigns a new value to an existing variant
object
1) Copy-assignment:
- If both
*this
andrhs
are valueless by exception, does nothing. - Otherwise, if
rhs
is valueless, but*this
is not, destroys the value contained in*this
and makes it valueless. - Otherwise, if
rhs
holds the same alternative as*this
, assigns the value contained inrhs
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. - Otherwise, if the alternative held by
other
is either nothrow copy constructible or not nothrow move constructible (as determined by std::is_nothrow_copy_constructible and std::is_nothrow_move_constructible, respectively), equivalent to this->emplace<other.index()>(get<other.index()>(other)). - Otherwise, equivalent to this->operator=(variant(rhs)).
This overload only participates in overload resolution if
std::is_copy_constructible_v<T_i> && std::is_copy_assignable_v<T_i>
is true
for all T_i
in Types...
2) Move-assignment:
- If both
*this
andrhs
are valueless by exception, does nothing - Otherwise, if
rhs
is valueless, but*this
is not, destroys the value contained in*this
and makes it valueless - Otherwise, if
rhs
holds the same alternative as*this
, assigns std::get<j>(std::move(rhs)) to the value contained in*this
, withj
beingindex()
. If an exception is thrown,*this
does not become valueless: the value depends on the exception safety guarantee of the alternative's move assignment. - Otherwise (if
rhs
and*this
hold different alternatives), equivalent to this->emplace<other.index()>(get<other.index()>(std::move(other))). If an exception is thrown byT_i
's move constructor,*this
becomes valueless_by_exception.
This overload only participates in overload resolution if
std::is_move_constructible_v<T_i> && std::is_move_assignable_v<T_i>
is true
for all T_i
in Types...
. 3) Converting assignment.
- Determines the alternative type
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 everyT_i
fromTypes...
in scope at the same time. - If
*this
already holds aT_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. - Otherwise, if std::is_nothrow_constructible_v<T_j, T> || !std::is_nothrow_move_constructible_v<T_j> is true, equivalent to this->emplace<j>(std::forward<T>(t));
- Otherwise, equivalent to this->operator=(variant(std::forward<T>(t))).
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
Parameters
rhs | - | another variant
|
t | - | a value convertible to one of the variant's alternatives |
Return value
*this
Exceptions
1) May throw any exception thrown by assignment and copy/move initialization of any alternative
2)
noexcept specification:
noexcept(((std::is_nothrow_move_constructible_v<Types> && std::is_nothrow_move_assignable_v<Types>) && ...))
3)
noexcept specification:
noexcept(std::is_nothrow_assignable_v<T_j&, T> && std::is_nothrow_constructible_v<T_j, T>)
Example
This section is incomplete Reason: no example |
See also
constructs a value in the variant, in place (public member function) |