Namespaces
Variants

std::expected<T,E>:: swap

From cppreference.net
Utilities library
Primäres Template
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 die Inhalte mit denen von other .

1) Die enthaltenen Werte werden wie folgt ausgetauscht:
Wert von
has_value()
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 ) ;
If has_value() is true and other. has_value ( ) is false , equivalent to:

// Fall 1: Die Move-Konstruktionen von Unexpected-Werten werfen keine Ausnahmen:
// „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-Konstruktionen von Expected-Werten werfen keine Ausnahmen:
// „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 ;

Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn alle folgenden Werte true sind:
2) Die unerwarteten Werte werden wie folgt ausgetauscht:
Wert von
has_value()
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 ) ;
Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn std:: is_swappable_v < E > und std:: is_move_constructible_v < E > beide true sind.

Inhaltsverzeichnis

Parameter

other - das expected Objekt, mit dem die Inhalte ausgetauscht werden sollen

Exceptions

Beispiel

#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{CAT FACE}");
    Ex ex2{"\N{GREEN HEART}"};
    show(ex1, ex2, "after ex1.swap(ex2):\n");
    ex1.swap(ex2);
    show(ex1, ex2, "\n\n");
    ex2 = std::unexpected(13);
    show(ex1, ex2, "after ex1.swap(ex2):\n");
    ex1.swap(ex2);
    show(ex1, ex2, "\n\n");
    ex2 = std::unexpected(37);
    show(ex1, ex2, "after ex1.swap(ex2):\n");
    ex1.swap(ex2);
    show(ex1, ex2);
}

Ausgabe:

ex1.value() = 🐱  ex2.value() = 💚  after ex1.swap(ex2):
ex1.value() = 💚  ex2.value() = 🐱 
ex1.value() = 💚  ex2.error() = 13  after ex1.swap(ex2):
ex1.error() = 13  ex2.value() = 💚 
ex1.error() = 13  ex2.error() = 37  after ex1.swap(ex2):
ex1.error() = 37  ex2.error() = 13
**Übersetzungsdetails:** - "Run this code" → "Diesen Code ausführen" - "Output:" → "Ausgabe:" - Alle HTML-Tags, Attribute und Code-Blöcke wurden unverändert beibehalten - C++-spezifische Begriffe (wie Funktionsnamen, Typen) wurden nicht übersetzt - Die Emoji-Zeichen und numerischen Werte bleiben erhalten - Die Formatierung und Struktur der Webseite wurde vollständig beibehalten

Siehe auch

spezialisiert den std::swap Algorithmus
(Funktion)