最終更新日時(UTC):
が更新

履歴 編集

function
<future>

std::promise::swap(C++11)

void swap(promise& other) noexcept;

概要

他のpromiseオブジェクトと値を入れ替える

効果

*thisotherの共有状態を入れ替える

事後条件

*thisはこの関数呼び出し前のotherの共有状態を持ち、otherはこの関数呼び出し前の*thisの共有状態を持つこと。

戻り値

なし

例外

投げない

#include <iostream>
#include <future>

int main()
{
  std::promise<int> p1;
  std::promise<int> p2;

  std::future<int> f1 = p1.get_future();
  std::future<int> f2 = p2.get_future();

  // 共有状態を入れ替える
  p1.swap(p2);

  p1.set_value(1);
  p2.set_value(2);

  // p1に書き込んだ値はf2、
  // p2に書き込んだ値はf1から取得される
  std::cout << f1.get() << std::endl;
  std::cout << f2.get() << std::endl;
}

出力

2
1

バージョン

言語

  • C++11

処理系

参照