std:: swap (std::optional)
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Observers | ||||
| Iterators | ||||
|
(C++26)
|
||||
|
(C++26)
|
||||
| Monadic operations | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
| Modifiers | ||||
| Non-member functions | ||||
|
swap
(std::optional)
|
||||
| Deduction guides | ||||
| Helper classes | ||||
| Helper objects | ||||
|
ヘッダーで定義
<optional>
|
||
|
template
<
class
T
>
void
swap
(
std::
optional
<
T
>
&
lhs,
|
(C++17以降)
(C++20以降constexpr) |
|
std::swap アルゴリズムを std::optional に対してオーバーロードします。 lhs の状態と rhs の状態を交換します。実質的には lhs. swap ( rhs ) を呼び出します。
このオーバーロードは、 std:: is_move_constructible_v < T > と std:: is_swappable_v < T > が両方とも true である場合にのみ、オーバーロード解決に参加します。
目次 |
パラメータ
| lhs, rhs | - |
optional
状態を交換するオブジェクト
|
戻り値
(なし)
例外
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_optional
|
202106L
|
(C++20)
(DR20) |
完全な constexpr |
例
#include <iostream> #include <optional> #include <string> int main() { std::optional<std::string> a{"██████"}, b{"▒▒▒▒▒▒"}; auto print = [&](auto const& s) { std::cout << s << "\t" "a = " << a.value_or("(null)") << " " "b = " << b.value_or("(null)") << '\n'; }; print("Initially:"); std::swap(a, b); print("swap(a, b):"); a.reset(); print("\n""a.reset():"); std::swap(a, b); print("swap(a, b):"); }
出力:
Initially: a = ██████ b = ▒▒▒▒▒▒ swap(a, b): a = ▒▒▒▒▒▒ b = ██████ a.reset(): a = (null) b = ██████ swap(a, b): a = ██████ b = (null)
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| P2231R1 | C++20 |
swap
は
constexpr
ではなかったが、必要な操作はC++20で
constexpr
にできる
|
constexpr に変更 |
関連項目
|
内容を交換する
(公開メンバ関数) |