Namespaces
Variants

swap(std::expected)

Aus de.cppreference.net
 
 
Dienstprogrammbibliothek
Sprachunterstützung
Typunterstützung(Basistypen, RTTI)
Bibliotheks-Feature-Test-Makros (C++20)
Programm-Dienstprogramme
Variadische Funktionen
Koroutinen-Unterstützung (C++20)
Vertragsunterstützung (C++26)
Drei-Wege-Vergleich
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

Allgemeine Dienstprogramme
Relationsoperatoren (veraltet in C++20)
Ganzzahlvergleichsfunktionen
(C++20)(C++20)(C++20)    
(C++20)
Swap und Typoperationen
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Allgemeine Vokabeltypen
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
friend constexpr void swap( expected& lhs, expected& rhs ) noexcept(/*see below*/);
(seit C++23)

Überlädt den std::swap Algorithmus für std::expected. Vertauscht den Zustand von lhs mit dem von rhs. Ruft effektiv lhs.swap(rhs) auf.

Diese Überladung nimmt nur an der Überladungsauflösung teil, wenn lhs.swap(rhs) gültig ist.

Diese Funktion ist bei der normalen unqualifizierten oder qualifizierten Namenssuche nicht sichtbar und kann nur durch argumentabhängige Namenssuche gefunden werden, wenn std::expected<T, E> eine assoziierte Klasse der Argumente ist.

Parameter

lhs, rhs - expected Objekte, deren Zustände ausgetauscht werden sollen

Rückgabewert

(keine)

Ausnahmen

noexcept Spezifikation:
noexcept ( noexcept ( lhs. swap ( rhs ) ) )

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{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);
}

Ausgabe:

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

Siehe auch

tauscht die Inhalte aus
(öffentliche Elementfunktion)