std::expected<T,E>::swap
| 主模板 |
||
| |
(1) | (C++23 起) |
void 部分特化 |
||
| |
(2) | (C++23 起) |
与 other 的内容进行交换。
has_value() 的值
|
other.has_value() 的值
| |
|---|---|---|
true
|
false
| |
true
|
using std::swap;swap(val, rhs.val);
|
见下文 |
false
|
other.swap(*this);
|
using std::swap;swap(unex, rhs.unex);
|
has_value() 是 true 而 other.has_value() 是 false,那么等价于:
// 情况 1:非预期值的移动构造不会抛出:
// 在 “other.val” 构造失败的情况下会复原 “other.unex”
if constexpr (std::is_nothrow_move_constructible_v<E>)
{
E temp(std::move(other.unex));
std::destroy_at(std::addressof(other.unex));
try
{
std::construct_at(std::addressof(other.val), std::move(val)); // 可能会抛出异常
std::destroy_at(std::addressof(val));
std::construct_at(std::addressof(unex), std::move(temp));
}
catch(...)
{
std::construct_at(std::addressof(other.unex), std::move(temp));
throw;
}
}
// 情况 2:预期值的移动构造不会抛出:
// 在 “this->unex” 构造失败的情况下会复原 “this->val”
else
{
T temp(std::move(val));
std::destroy_at(std::addressof(val));
try
{
std::construct_at(std::addressof(unex), std::move(other.unex)); // 可能会抛出异常
std::destroy_at(std::addressof(other.unex));
std::construct_at(std::addressof(other.val), std::move(temp));
}
catch(...)
{
std::construct_at(std::addressof(val), std::move(temp));
throw;
}
}
has_val = false;
rhs.has_val = true;
true 时才会参与重载决议:
std::is_swappable_v<T>std::is_swappable_v<E>std::is_move_constructible_v<T> && std::is_move_constructible_v<E>std::is_nothrow_move_constructible_v<T> || std::is_nothrow_move_constructible_v<E>
has_value() 的值
|
other.has_value() 的值
| |
|---|---|---|
true
|
false
| |
true
|
using std::swap;swap(val, rhs.val);
|
std::construct_at(std::addressof(unex), std::move(rhs.unex));std::destroy_at(std::addressof(rhs.unex));has_val = false;rhs.has_val = true;
|
false
|
other.swap(*this);
|
using std::swap;swap(unex, rhs.unex);
|
std::is_swappable_v<E> 和 std::is_move_constructible_v<E> 都是 true 时才会参与重载决议。参数
| other | - | 要与之交换内容的 expected 对象
|
异常
noexcept( std::is_nothrow_move_constructible_v<T> && std::is_nothrow_swappable_v<T> && std::is_nothrow_move_constructible_v<E> && std::is_nothrow_swappable_v<E> )noexcept( std::is_nothrow_move_constructible_v<E> && std::is_nothrow_swappable_v<E> )示例
#include <expected>
#include <iostream>
#include <string>
using Ex = std::expected<std::string, int>;
void show(const Ex& ex1, const Ex& ex2)
{
for (int i{}; i < 2; ++i)
{
std::cout << (i ? "ex2" : "ex1");
if (const Ex& ex = (i ? ex2 : ex1); ex.has_value())
std::cout << ".has_value() = " << *ex << '\n';
else
std::cout << ".error() = " << ex.error() << '\n';
}
}
int main()
{
Ex ex1("\N{CAT FACE}");
Ex ex2{"\N{GREEN HEART}"};
show(ex1, ex2);
ex1.swap(ex2);
std::cout << "ex1.swap(ex2);\n";
show(ex1, ex2);
std::cout << '\n';
ex2 = std::unexpected(13);
show(ex1, ex2);
std::cout << "ex1.swap(ex2);\n";
ex1.swap(ex2);
show(ex1, ex2);
std::cout << '\n';
ex2 = std::unexpected(19937);
show(ex1, ex2);
std::cout << "ex1.swap(ex2);\n";
ex1.swap(ex2);
show(ex1, ex2);
}输出:
ex1.has_value() = 🐱
ex2.has_value() = 💚
ex1.swap(ex2);
ex1.has_value() = 💚
ex2.has_value() = 🐱
ex1.has_value() = 💚
ex2.error() = 13
ex1.swap(ex2);
ex1.error() = 13
ex2.has_value() = 💚
ex1.error() = 13
ex2.error() = 19937
ex1.swap(ex2);
ex1.error() = 19937
ex2.error() = 13参阅
(C++23) |
特化 std::swap 算法 (函数) |