Namespaces
Variants

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

Depuis fr.cppreference.net
 
 
Bibliothèque d'utilitaires
Support du langage
Support des types (types de base, RTTI)
Macros de test de fonctionnalités de la bibliothèque (C++20)
Utilitaires de programme
Fonctions variadiques
Support des coroutines (C++20)
Support des contrats (C++26)
Comparaison à trois voies
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

Utilitaires généraux
Opérateurs relationnels (obsolète en C++20)
Fonctions de comparaison d'entiers
(C++20)(C++20)(C++20)    
(C++20)
Opérations d'échange et de type
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Types de vocabulaire courants
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
Modèle principal
constexpr void swap( expected& other ) noexcept(/* see below */);
(1) (depuis C++23)
void spécialisation partielle
constexpr void swap( expected& other ) noexcept(/* see below */);
(2) (depuis C++23)

Échange le contenu avec celui de other.

1) Les valeurs contenues sont échangées comme suit :
Valeur de
 has_value() 
Valeur de other.has_value()
true false
true using std::swap;
swap(val, rhs.val);
voir ci-dessous
false other.swap(*this); using std::swap;
swap(unex, rhs.unex);
Si has_value() est true et other.has_value() est false, équivalent à :

// Case 1: the move constructions of unexpected values are non-throwing:
// “other.unex” will be restored if the construction of “other.val” fails
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)); // may throw
        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;
    }
}
// Case 2: the move constructions of expected values are non-throwing:
// “this->val” will be restored if the construction of “this->unex” fails
else
{
    T temp(std::move(val));
    std::destroy_at(std::addressof(val));
    try
    {
        std::construct_at(std::addressof(unex), std::move(other.unex)); // may throw 
        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;

Cette surcharge participe à la résolution de surcharge uniquement si toutes les valeurs suivantes sont 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) Les valeurs inattendues sont échangées comme suit :
Valeur de
 has_value() 
Valeur 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);
Cette surcharge participe à la résolution de surcharge uniquement si std::is_swappable_v<E> et std::is_move_constructible_v<E> sont tous deux true.

Paramètres

autre - l'objet expected avec lequel échanger le contenu

Exceptions

Exemple

#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, "après ex1.swap(ex2):\n");
    ex1.swap(ex2);
    show(ex1, ex2, "\n\n");
    ex2 = std::unexpected(13);
    show(ex1, ex2, "après ex1.swap(ex2):\n");
    ex1.swap(ex2);
    show(ex1, ex2, "\n\n");
    ex2 = std::unexpected(37);
    show(ex1, ex2, "après ex1.swap(ex2):\n");
    ex1.swap(ex2);
    show(ex1, ex2);
}

Sortie :

ex1.value() = 🐱  ex2.value() = 💚  après ex1.swap(ex2):
ex1.value() = 💚  ex2.value() = 🐱 
ex1.value() = 💚  ex2.error() = 13  après ex1.swap(ex2):
ex1.error() = 13  ex2.value() = 💚 
ex1.error() = 13  ex2.error() = 37  après ex1.swap(ex2):
ex1.error() = 37  ex2.error() = 13

Voir aussi

spécialise l'algorithme std::swap
(fonction)