std::variant<Types...>::operator=
来自 cppreference.cn
constexpr variant& operator=( const variant& rhs ); |
(1) | (C++17 起) |
constexpr variant& operator=( variant&& rhs ) noexcept(/* 见下文 */); |
(2) | (C++17 起) |
template< class T > variant& operator=( T&& t ) noexcept(/* 见下文 */); |
(3) | (C++17 起) (C++20 起为 constexpr) |
为现有 variant
对象赋值。
1) 拷贝赋值
- 如果 *this 和 rhs 都因异常而无值,则不执行任何操作。
- 否则,如果 rhs 无值,但 *this 有值,则销毁 *this 中包含的值并使其无值。
- 否则,如果 rhs 包含的备选项与 *this 相同,则将 rhs 中包含的值赋给 *this 中包含的值。如果抛出异常,*this 不会变为无值:值取决于备选项的拷贝赋值的异常安全保证。
- 否则,如果 rhs 持有的备选项是 nothrow 拷贝可构造的,或者 不是 nothrow 移动可构造的(分别由 std::is_nothrow_copy_constructible 和 std::is_nothrow_move_constructible 确定),则等价于 this->emplace<rhs.index()>(*std::get_if<rhs.index()>(std::addressof(rhs)))。*this 可能会变为
valueless_by_exception
,如果在emplace
内部的拷贝构造过程中抛出异常。 - 否则,等价于 this->operator=(variant(rhs))。
除非
Types...
中所有 T_i
的 std::is_copy_constructible_v<T_i> 和 std::is_copy_assignable_v<T_i> 都为 true,否则此重载被定义为删除。如果 Types...
中所有 T_i
的 std::is_trivially_copy_constructible_v<T_i>、std::is_trivially_copy_assignable_v<T_i> 和 std::is_trivially_destructible_v<T_i> 都为 true,则此重载是平凡的。2) 移动赋值
- 如果 *this 和 rhs 都因异常而无值,则不执行任何操作。
- 否则,如果 rhs 无值,但 *this 有值,则销毁 *this 中包含的值并使其无值。
- 否则,如果 rhs 包含的备选项与 *this 相同,则将 std::move(*std::get_if<j>(std::addressof(rhs))) 赋给 *this 中包含的值,其中
j
为index()
。如果抛出异常,*this 不会变为无值:值取决于备选项的移动赋值的异常安全保证。 - 否则(如果 rhs 和 *this 包含不同的备选项),等价于 this->emplace<rhs.index()>(std::move(*std::get_if<rhs.index()>(std::addressof(rhs))))。如果
T_i
的移动构造函数抛出异常,*this 将变为valueless_by_exception
。
仅当
Types...
中所有 T_i
的 std::is_move_constructible_v<T_i> 和 std::is_move_assignable_v<T_i> 都为 true 时,此重载才参与重载决议。如果 Types...
中所有 T_i
的 std::is_trivially_move_constructible_v<T_i>、std::is_trivially_move_assignable_v<T_i> 和 std::is_trivially_destructible_v<T_i> 都为 true,则此重载是平凡的。3) 转换赋值。
- 确定如果对于
Types...
中每个T_i
都存在一个虚构函数 F(T_i) 的重载,表达式 F(std::forward<T>(t)) 将通过重载决议选择的备选项类型T_j
,但以下情况除外:
- 仅当声明 T_i x[] = { std::forward<T>(t) }; 对于某个虚构变量
x
有效时,才考虑重载 F(T_i);
- 仅当声明 T_i x[] = { std::forward<T>(t) }; 对于某个虚构变量
- 如果 *this 已包含一个
T_j
,则将 std::forward<T>(t) 赋给 *this 中包含的值。如果抛出异常,*this 不会变为无值:值取决于被调用的赋值的异常安全保证。 - 否则,如果 std::is_nothrow_constructible_v<T_j, T> || !std::is_nothrow_move_constructible_v<T_j> 为 true,则等价于 this->emplace<j>(std::forward<T>(t))。*this 可能会变为
valueless_by_exception
,如果在emplace
内部的初始化过程中抛出异常。 - 否则,等价于 this->emplace<j>(T_j(std::forward<T>(t)))。
仅当 std::decay_t<T>(直到 C++20)std::remove_cvref_t<T>(C++20 起) 与 variant
类型不同,且 std::is_assignable_v<T_j&, T> 为 true,且 std::is_constructible_v<T_j, T> 为 true,并且表达式 F(std::forward<T>(t))(其中 F 是上述虚构函数集)格式正确时,此重载才参与重载决议。
std::variant<std::string> v1; v1 = "abc"; // OK std::variant<std::string, std::string> v2; v2 = "abc"; // Error std::variant <std::string, bool> v3; v3 = "abc"; // OK, chooses string; bool is not a candidate std::variant<float, long, double> v4; // holds float v4 = 0; // OK, holds long; float and double are not candidates
目录 |
[编辑] 参数
rhs | - | 另一个 variant |
t | - | 一个可转换为 variant 某个备选项的值 |
[编辑] 返回值
*this
[编辑] 异常
1) 可能抛出任何由任何备选项的赋值和拷贝/移动初始化抛出的异常。
2)
noexcept 规范:
noexcept(((std::is_nothrow_move_constructible_v<Types> &&
std::is_nothrow_move_assignable_v<Types>) && ...))
std::is_nothrow_move_assignable_v<Types>) && ...))
3)
noexcept 规范:
noexcept(std::is_nothrow_assignable_v<T_j&, T> &&
std::is_nothrow_constructible_v<T_j, T>)
std::is_nothrow_constructible_v<T_j, T>)
[编辑] 注意
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_lib_variant |
202106L |
(C++20) (DR) |
完全 constexpr 的 std::variant (3) |
[编辑] 示例
运行此代码
#include <iomanip> #include <iostream> #include <string> #include <type_traits> #include <variant> std::ostream& operator<<(std::ostream& os, std::variant<int, std::string> const& va) { os << ": { "; std::visit([&](auto&& arg) { using T = std::decay_t<decltype(arg)>; if constexpr (std::is_same_v<T, int>) os << arg; else if constexpr (std::is_same_v<T, std::string>) os << std::quoted(arg); }, va); return os << " };\n"; } int main() { std::variant<int, std::string> a{2017}, b{"CppCon"}; std::cout << "a" << a << "b" << b << '\n'; std::cout << "(1) operator=( const variant& rhs )\n"; a = b; std::cout << "a" << a << "b" << b << '\n'; std::cout << "(2) operator=( variant&& rhs )\n"; a = std::move(b); std::cout << "a" << a << "b" << b << '\n'; std::cout << "(3) operator=( T&& t ), where T is int\n"; a = 2019; std::cout << "a" << a << '\n'; std::cout << "(3) operator=( T&& t ), where T is std::string\n"; std::string s{"CppNow"}; std::cout << "s: " << std::quoted(s) << '\n'; a = std::move(s); std::cout << "a" << a << "s: " << std::quoted(s) << '\n'; }
可能的输出
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: ""
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 3024 | C++17 | 如果任何成员类型不可拷贝, 拷贝赋值运算符不参与重载决议, |
而是定义为删除。 |
LWG 3585 | C++17 | 转换赋值有时会意外地格式错误, 因为没有可用的移动赋值。 |
使其格式正确 |
P0602R4 | C++17 | 拷贝/移动赋值可能不是平凡的, 即使底层操作是平凡的。 |
要求传播平凡性 |
P0608R3 | C++17 | 转换赋值盲目地组建重载集, 导致意外转换。 |
窄化和布尔转换 不考虑 |
P2231R1 | C++20 | 转换赋值 (3) 不是 constexpr 而在 C++20 中所需操作可以是 constexpr 的。 |
设为 constexpr |
[编辑] 参阅
在 variant 中原地构造一个值(公共成员函数) |