std::expected<T,E>::swap
| Primäre Vorlage |
||
| constexpr void swap( expected& other ) noexcept(/* siehe unten */); |
(1) | (seit C++23) |
| void partielle Spezialisierung |
||
| constexpr void swap( expected& other ) noexcept(/* siehe unten */); |
(2) | (seit C++23) |
Tauscht den Inhalt mit dem von other.
Wert vonhat_wert()
|
Wert von other.has_value() | |
|---|---|---|
| true | false | |
| true | using std::swap; swap( val, rhs.val);
|
siehe unten |
| false | other.swap(*this); | using std::swap; swap( unex, rhs.unex);
|
has_value() true ist und other.has_value() false ist, äquivalent zu
// Fall 1: Die Move-Konstruktion von unerwarteten Werten ist nicht werfend
// "other.unex" wird wiederhergestellt, falls die Konstruktion von "other.val" fehlschlägt
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)); // kann werfen
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;
}
}
// Fall 2: Die Move-Konstruktion von erwarteten Werten ist nicht werfend
// "this->val" wird wiederhergestellt, falls die Konstruktion von "this->unex" fehlschlägt
else
{
T temp(std::move(val));
std::destroy_at(std::addressof(val));
try
{
std::construct_at(std::addressof(unex), std::move(other.unex)); // kann werfen
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;
Wert vonhat_wert()
|
Wert von 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);
|
Inhalt |
[edit] Parameter
| Sonstiges | - | das expected-Objekt, dessen Inhalt ausgetauscht werden soll |
[edit] Ausnahmen
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>
std::is_nothrow_move_constructible_v<E> && std::is_nothrow_swappable_v<E>
[edit] Beispiel
#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); }
Ausgabe
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
[edit] Siehe auch
| (C++23) |
spezialisiert den Algorithmus std::swap (Funktion) |