constexpr variant& operator=( const variant& rhs );
(1) (since C++17)constexpr variant& operator=( variant&& rhs ) noexcept(/* see below */);
(2) (since C++17)template< class T >
variant& operator=( T&& t ) noexcept(/* see below */);
Assigns a new value to an existing variant
object.
Copy-assignment:
valueless_by_exception
if an exception is thrown on the copy-construction inside emplace
.Move-assignment:
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.T_i
's move constructor, *this becomes valueless_by_exception
.Converting assignment.
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, except that:x
;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.valueless_by_exception
if an exception is thrown on the initialization inside emplace
.This overload participates in overload resolution only if std::decay_t<T>(until C++20)std::remove_cvref_t<T>(since C++20) is not the same type as variant
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
t - a value convertible to one of the variant's alternatives [edit] Return value
*this
[edit] Exceptions1) May throw any exception thrown by assignment and copy/move initialization of any alternative.
[edit] Notes [edit] ExamplePossible output:
a: { 2017 }; b: { "CppCon" }; (1) operator=( const variant& rhs ) a: { "CppCon" }; b: { "CppCon" }; (2) operator=( variant&& rhs ) a: { "CppCon" }; b: { "" }; (3) operator=( T&& t ), where T is int a: { 2019 }; (3) operator=( T&& t ), where T is std::string s: "CppNow" a: { "CppNow" }; s: ""[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 3024 C++17 copy assignment operator doesn't participate in overload resolutionvariant
, in place
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4