forked from 0voice/cpp_new_features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path004_rtti_compare_std_coroutine_traits.cpp
More file actions
97 lines (86 loc) · 2.69 KB
/
004_rtti_compare_std_coroutine_traits.cpp
File metadata and controls
97 lines (86 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <chrono>
#include <coroutine>
#include <exception>
#include <future>
#include <iostream>
#include <thread>
#include <type_traits>
// Enable the use of std::future<T> as a coroutine type
// by using a std::promise<T> as the promise type.
template <typename T, typename... Args>
requires(!std::is_void_v<T> && !std::is_reference_v<T>)
struct std::coroutine_traits<std::future<T>, Args...> {
struct promise_type : std::promise<T> {
std::future<T> get_return_object() noexcept {
return this->get_future();
}
std::suspend_never initial_suspend() const noexcept { return {}; }
std::suspend_never final_suspend() const noexcept { return {}; }
void return_value(const T &value)
noexcept(std::is_nothrow_copy_constructible_v<T>) {
this->set_value(value);
}
void return_value(T &&value)
noexcept(std::is_nothrow_move_constructible_v<T>) {
this->set_value(std::move(value));
}
void unhandled_exception() noexcept {
this->set_exception(std::current_exception());
}
};
};
// Same for std::future<void>.
template <typename... Args>
struct std::coroutine_traits<std::future<void>, Args...> {
struct promise_type : std::promise<void> {
std::future<void> get_return_object() noexcept {
return this->get_future();
}
std::suspend_never initial_suspend() const noexcept { return {}; }
std::suspend_never final_suspend() const noexcept { return {}; }
void return_void() noexcept {
this->set_value();
}
void unhandled_exception() noexcept {
this->set_exception(std::current_exception());
}
};
};
// Allow co_await'ing std::future<T> and std::future<void>
// by naively spawning a new thread for each co_await.
template <typename T>
auto operator co_await(std::future<T> future) noexcept
requires(!std::is_reference_v<T>) {
struct awaiter : std::future<T> {
bool await_ready() const noexcept {
using namespace std::chrono_literals;
return this->wait_for(0s) != std::future_status::timeout;
}
void await_suspend(std::coroutine_handle<> cont) const {
std::thread([this, cont] {
this->wait();
cont();
}).detach();
}
T await_resume() { return this->get(); }
};
return awaiter{std::move(future)};
}
// Utilize the infrastructure we have established.
std::future<int> compute() {
int a = co_await std::async([] { return 6; });
int b = co_await std::async([] { return 7; });
co_return a * b;
}
std::future<void> fail() {
throw std::runtime_error("bleah");
co_return;
}
int main() {
std::cout << compute().get() << '\n';
try {
fail().get();
} catch (const std::runtime_error &e) {
std::cout << "error: " << e.what() << '\n';
}
}