Skip to content

Latest commit

 

History

History
113 lines (89 loc) · 2.29 KB

File metadata and controls

113 lines (89 loc) · 2.29 KB

swap (非メンバ関数)

  • optional[meta header]
  • std[meta namespace]
  • function template[meta id-type]
  • cpp17[meta cpp]
namespace std {
  template <class T>
  void swap(optional<T>& x, optional<T>& y) noexcept(noexcept(x.swap(y)));           // C++17
  template <class T>
  constexpr void swap(optional<T>& x, optional<T>& y) noexcept(noexcept(x.swap(y))); // C++23
}
  • x.swap(y)[link swap.md]

概要

2つのoptionalオブジェクトを入れ替える。

効果

x.swap(y);
  • swap[link swap.md]

備考

Tがムーブ構築できない、もしくは型Tがswap可能でない場合、この関数はオーバーロード解決の候補から除外される。

#include <cassert>
#include <optional>

int main()
{
  // 状況1
  // 左辺と右辺の両方が有効値を持つ場合
  {
    std::optional<int> a = 3;
    std::optional<int> b = 1;

    // aとbを入れ替える
    std::swap(a, b);

    assert(a.value() == 1);
    assert(b.value() == 3);
  }

  // 状況2
  // 左辺が有効値を持ち、右辺が有効値を持たない場合
  {
    std::optional<int> a = 3;
    std::optional<int> b;

    // aとbを入れ替える
    std::swap(a, b);

    assert(!a.has_value());
    assert(b.has_value());
    assert(b.value() == 3);
  }

  // 状況3
  // 左辺が有効値を持たず、右辺が有効値を持つ場合
  {
    std::optional<int> a;
    std::optional<int> b = 3;

    // aとbを入れ替える
    std::swap(a, b);

    assert(a.has_value());
    assert(a.value() == 3);
    assert(!b.has_value());
  }

  // 状況4
  // 左辺と右辺の両方が有効値を持たない場合
  {
    std::optional<int> a;
    std::optional<int> b;

    // aとbを入れ替える
    std::swap(a, b);

    assert(!a.has_value());
    assert(!b.has_value());
  }
}
  • std::swap[color ff0000]
  • has_value()[link has_value.md]
  • value()[link value.md]

出力

バージョン

言語

  • C++17

処理系

参照