Espacios de nombres
Variantes

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

De cppreference.com
 
 
Biblioteca de servicios
Apoyo del lenguaje
Apoyo de tipos (tipos básicos, RTTI)
Macros de prueba de característica de la biblioteca (C++20)
Servicios de programa
Funciones variádicas
Apoyo de corrutinas (C++20)
Apoyo de contratos (C++26)
Comparación de tres vías (C++20)
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

 
 
Plantilla primaria
constexpr void swap( expected& other ) noexcept(/* véase más abajo */);
(1) (desde C++23)
Especialización parcial void
constexpr void swap( expected& other ) noexcept(/* véase más abajo */);
(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);
véase más abajo
false other.swap(*this); using std::swap;
swap(unex, rhs.unex);
Si has_value() es true y other.has_value() es false, equivalente a:

// Caso 1: las construcciones por movimiento de los valores no esperados
// no lanzan excepciones:
// “other.unex” se restaurará 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(other.unex));
    try
    {
        std::construct_at(std::addressof(other.val), std::move(val)); // puede lanzar
        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;
    }
}
// Caso 2: las construcciones por movimiento de los valores esperados
// no lanzan excepciones:
// “this->val” se restaurará 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 
        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;

Esta sobrecarga solo participa en la resolución de sobrecargas si todos los valores siguientes son 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>
2) Los valores no esperados 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 solo participa en la resolución de sobrecargas si std::is_swappable_v<E> y std::is_move_constructible_v<E> son ambos true.

Parámetros

other - El objeto expected con el que intercambiar el contenido.

Excepciones

1)
Especificación noexcept:   (desde C++11)
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>
)
2)
Especificación noexcept:   (desde C++11)
noexcept(
    std::is_nothrow_move_constructible_v<E> && std::is_nothrow_swappable_v<E>
)

Ejemplo

#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{CARA DE GATO}");
    Ex ex2{"\N{CORAZÓN VERDE}"};
    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);
}

Salida:

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

Véase también

Especializa el algoritmo std::swap.
(función) [editar]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.