名前空間
変種
操作

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

From cppreference.com
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
 
プライマリテンプレート
constexpr void swap( expected& other ) noexcept(/* 下記参照 */);
(1) (C++23から)
void 部分特殊化
constexpr void swap( expected& other ) noexcept(/* 下記参照 */);
(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.unex” は “other.val” の構築が失敗した場合に復元されます。
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->val” は “this->unex” の構築が失敗した場合に復元されます。
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;

このオーバーロードは、以下のすべての値が 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>
 
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{CAT FACE}");
    Ex ex2{"\N{GREEN HEART}"};
    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);
}

出力

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

[編集] 関連項目

std::swap アルゴリズムを特殊化する
(function) [編集]
English 日本語 中文(简体) 中文(繁體)