swap (std::expected)
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Observers | ||||
| Monadic operations | ||||
| Modifiers | ||||
| Non-member functions | ||||
|
swap
(std::expected)
|
||||
| Helper classes | ||||
|
friend
constexpr
void
swap
(
expected
&
lhs, expected
&
rhs
)
noexcept
(
/*see below*/
)
;
|
(自 C++23 起) | |
为 std::expected 重载 std::swap 算法。交换 lhs 与 rhs 的状态。实际调用 lhs. swap ( rhs ) 。
此重载仅当 lhs. swap ( rhs ) 有效时才参与重载决议。
此函数对普通的 非限定查找 或 限定查找 不可见,仅当 std:: expected < T, E > 作为参数的关联类时,才能通过 实参依赖查找 找到。
目录 |
参数
| lhs, rhs | - |
要交换状态的
expected
对象
|
返回值
(无)
异常
示例
#include <expected> #include <iostream> #include <string_view> using Ex = std::expected<std::string, int>; void show(const Ex& ex1, const Ex& ex2, std::string_view term = "\n") { for (int i{}; i != 2; ++i) { std::cout << (i ? "ex2" : "ex1"); if (const Ex& ex = (i ? ex2 : ex1); ex.has_value()) std::cout << ".value() = " << *ex << " "; else std::cout << ".error() = " << ex.error() << " "; } std::cout << term; } int main() { Ex ex1("\N{SMILING FACE WITH SUNGLASSES}"); Ex ex2{"\N{SLIGHTLY SMILING FACE}"}; show(ex1, ex2, "after swap(ex1, ex2):\n"); std::swap(ex1, ex2); show(ex1, ex2, "\n\n"); ex2 = std::unexpected(13); show(ex1, ex2, "after swap(ex1, ex2):\n"); std::swap(ex1, ex2); show(ex1, ex2, "\n\n"); ex2 = std::unexpected(37); show(ex1, ex2, "after swap(ex1, ex2):\n"); std::swap(ex1, ex2); show(ex1, ex2); }
输出:
ex1.value() = 😎 ex2.value() = 🙂 after swap(ex1, ex2): ex1.value() = 🙂 ex2.value() = 😎 ex1.value() = 🙂 ex2.error() = 13 after swap(ex1, ex2): ex1.error() = 13 ex2.value() = 🙂 ex1.error() = 13 ex2.error() = 37 after swap(ex1, ex2): ex1.error() = 37 ex2.error() = 13
参阅
|
交换内容
(公开成员函数) |