Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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) };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement