Namespaces
Variants

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

From cppreference.net
Utilities library
Plantilla principal
constexpr void swap ( expected & other ) noexcept ( /* see below */ ) ;
(1) (desde C++23)
void Especialización parcial
constexpr void swap ( expected & other ) noexcept ( /* see below */ ) ;
(2) (desde C++23)

Intercambia el contenido con el de other .

1) Los valores contenidos se intercambian de la siguiente manera:
Valor de
has_value()
Valor de other. has_value ( )
true false
true using std:: swap ;
swap ( val , rhs. val ) ;
ver más abajo
false other. swap ( * this ) ; using std:: swap ;
swap ( unex , rhs. unex ) ;
If has_value() is true and otro. has_value ( ) is false , equivalent to:

// Caso 1: las construcciones por movimiento de valores inesperados no lanzan excepciones:
// “other.unex” será restaurado si la construcción de “other.val” falla
if constexpr ( std:: is_nothrow_move_constructible_v < E > )
{
E temp ( std :: move ( other. unex ) ) ;
std:: destroy_at ( std:: addressof ( otro. unex ) ) ;
try
{
std:: construct_at ( std:: addressof ( other. val ) , std :: move ( val ) ) ; // puede lanzar excepción
std:: destroy_at ( std:: addressof ( val ) ) ;
std:: construct_at ( std:: addressof ( unex ) , std :: move ( temp ) ) ;
}
catch ( ... )
{
std:: construct_at ( std:: addressof ( otro. unex ) , std :: move ( temp ) ) ;
throw ;
}
}
// Caso 2: las construcciones por movimiento de valores esperados no lanzan excepciones:
// “this->val” será restaurado si la construcción de “this->unex” falla
else
{
T temp ( std :: move ( val ) ) ;
std:: destroy_at ( std:: addressof ( val ) ) ;
try
{
std:: construct_at ( std:: addressof ( unex ) , std :: move ( other. unex ) ) ; // puede lanzar excepción
std:: destroy_at ( std:: addressof ( otro. unex ) ) ;
std:: construct_at ( std:: addressof ( otro. val ) , std :: move ( temp ) ) ;
}
catch ( ... )
{
std:: construct_at ( std:: addressof ( val ) , std :: move ( temp ) ) ;
throw ;
}
}
has_val = false ;
rhs. has_val = true ;

Esta sobrecarga participa en la resolución de sobrecarga solo si todos los siguientes valores son true :
2) Los valores inesperados se intercambian de la siguiente manera:
Valor de
has_value()
Valor de 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 ) ;
Esta sobrecarga participa en la resolución de sobrecarga solo si std:: is_swappable_v < E > y std:: is_move_constructible_v < E > son ambos true .

Contenidos

Parámetros

otro - el objeto expected con el cual intercambiar los contenidos

Excepciones

Ejemplo

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

Salida:

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

Véase también

especializa el algoritmo std::swap
(función)