Advertisement
Zgragselus

Untitled

Jul 29th, 2024
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. template<typename T>
  2. auto operator co_await(std::future<T> future) noexcept
  3.     requires(!std::is_reference_v<T>)
  4. {
  5.     struct awaiter : std::future<T>
  6.     {
  7.         bool await_ready() const noexcept
  8.         {
  9.             using namespace std::chrono_literals;
  10.             return this->wait_for(0s) != std::future_status::timeout;
  11.         }
  12.  
  13.         void await_suspend(std::coroutine_handle<> cont) const
  14.         {
  15.             std::thread([this, cont]
  16.             {
  17.                 this->wait();
  18.                 cont();
  19.             }).detach();
  20.         }
  21.  
  22.         T await_resume() { return this->get(); }
  23.     };
  24.  
  25.     return awaiter { std::move(future) };
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement