Namespaces
Variants
Views
Actions

swap(std::expected)

From cppreference.com
< cpp‎ | utility‎ | expected
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
(C++26)
Three-way comparison
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)

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

Overloads the std::swap algorithm for std::expected. Exchanges the state of lhs with that of rhs. Effectively calls lhs.swap(rhs).

This overload participates in overload resolution only if lhs.swap(rhs) is valid.

This function is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::expected<T, E> is an associated class of the arguments.

Contents

[edit] Parameters

lhs, rhs - expected objects whose states to swap

[edit] Return value

(none)

[edit] Exceptions

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

[edit] Example

#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{DOG FACE}");
    Ex ex2{"\N{BONE}"};
    show(ex1, ex2);
    swap(ex1, ex2);
    std::cout << "swap(ex1, ex2);\n";
    show(ex1, ex2);
    std::cout << '\n';
 
    ex2 = std::unexpected(13);
    show(ex1, ex2);
    swap(ex1, ex2);
    std::cout << "swap(ex1, ex2);\n";
    show(ex1, ex2);
    std::cout << '\n';
 
    ex2 = std::unexpected(19937);
    show(ex1, ex2);
    swap(ex1, ex2);
    std::cout << "swap(ex1, ex2);\n";
    show(ex1, ex2);
    std::cout << '\n';
}

Output:

ex1.has_value() = 🐶
ex2.has_value() = 🦴
swap(ex1, ex2);
ex1.has_value() = 🦴
ex2.has_value() = 🐶
 
ex1.has_value() = 🦴
ex2.error() = 13
swap(ex1, ex2);
ex1.error() = 13
ex2.has_value() = 🦴
 
ex1.error() = 13
ex2.error() = 19937
swap(ex1, ex2);
ex1.error() = 19937
ex2.error() = 13

[edit] See also

exchanges the contents
(public member function) [edit]