Namespaces
Variants

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

From cppreference.net
Utilities library
主模板
constexpr void swap ( expected & other ) noexcept ( /* see below */ ) ;
(1) (自 C++23 起)
void 部分特化
constexpr void swap ( expected & other ) noexcept ( /* see below */ ) ;
(2) (自 C++23 起)

other 交换内容。

1) 所含值的交换方式如下:
has_value() 的值
other. has_value ( ) 的值
true false
true using std:: swap ;
swap ( val , rhs. val ) ;
见下文
false other. swap ( * this ) ; using std:: swap ;
swap ( unex , rhs. unex ) ;
has_value() true other. has_value ( ) false ,则等价于:

// 情况1:异常值的移动构造为不抛出的:
// 若“other.val”构造失败,“other.unex”将被恢复
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 ) ) ; // 可能抛出异常
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 ;
}
}
// 情况2:期望值的移动构造为不抛出的:
// 若“this->unex”构造失败,“this->val”将被恢复
else
{
T temp ( std :: move ( val ) ) ;
std:: destroy_at ( std:: addressof ( val ) ) ;
try
{
std:: construct_at ( std:: addressof ( unex ) , std :: move ( other. unex ) ) ; // 可能抛出异常
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 ;

2) 意外值按以下方式交换:
has_value() 的值 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 ) ;
此重载仅当 std:: is_swappable_v < E > std:: is_move_constructible_v < E > 均为 true 时参与重载决议。

目录

参数

其他 - 用于交换内容的 expected 对象

异常

示例

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

输出:

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

参见

特化 std::swap 算法
(函数)