swap(std::expected)
From cppreference.com
| friend constexpr void swap( expected& lhs, expected& rhs ) noexcept(/*下記参照*/); |
(C++23から) | |
std::swap アルゴリズムを std::expected 用にオーバーロードします。 lhs の状態と rhs の状態を交換します。 実質的に lhs.swap(rhs) を呼び出します。
lhs.swap(rhs) が有効な場合にのみ、このオーバーロードはオーバーロード解決に参加します。
この関数は、通常の非修飾または修飾検索からは見えず、std::expected<T, E> が引数の関連クラスである場合にのみ、引数依存名前空間検索によって見つけることができます。
目次 |
[編集] Parameters
| lhs, rhs | - | 状態を交換する expected オブジェクト |
[編集] Return value
(なし)
[編集] Exceptions
noexcept 指定:
noexcept(noexcept(lhs.swap(rhs)))
[編集] 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'; }
出力
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
[編集] See also
| 内容を交換する (public member function) |