Skip to content

Commit

Permalink
promiseデストラクタが例外を格納するコード例を作成 #1270
Browse files Browse the repository at this point in the history
  • Loading branch information
tshino committed Apr 30, 2024
1 parent eddd595 commit 8e854b8
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions reference/future/promise/op_destructor.md
Expand Up @@ -18,11 +18,44 @@
2. 共有状態を解放する。

##
```cpp
```cpp example
#include <iostream>
#include <future>
#include <thread>
#include <utility>

void calc(std::promise<int> p)
{
// 通常行う p.set_value() をしなかったとする

// ここで promise のデストラクタが例外オブジェクトを書き込む
}

int main()
{
std::promise<int> p;
std::future<int> f = p.get_future();

std::thread t(calc, std::move(p));

try {
std::cout << f.get() << std::endl; // 上で書き込まれた例外が送出される
}
catch (std::future_error& e) {
std::cout << e.what() << std::endl;
}

t.join();
}
```
* std::future[link /reference/future/future.md]
* p.get_future()[link get_future.md]
* std::move[link /reference/utility/move.md]
* f.get()[link /reference/future/future/get.md]
### 出力
### 出力例
```
std::future_error: Broken promise
```
## バージョン
Expand Down

0 comments on commit 8e854b8

Please sign in to comment.