| 1 |
// { dg-do run } |
| 2 |
// { dg-additional-options "-std=c++20" } |
| 3 |
|
| 4 |
#include <coroutine> |
| 5 |
#ifdef OUTPUT |
| 6 |
#include <iostream> |
| 7 |
#endif |
| 8 |
|
| 9 |
struct Promise; |
| 10 |
|
| 11 |
struct Handle : std::coroutine_handle<Promise> { |
| 12 |
using promise_type = Promise; |
| 13 |
}; |
| 14 |
|
| 15 |
struct Promise { |
| 16 |
Handle get_return_object() noexcept { |
| 17 |
return {Handle::from_promise(*this)}; |
| 18 |
} |
| 19 |
std::suspend_never initial_suspend() const noexcept { return {}; } |
| 20 |
std::suspend_never final_suspend() const noexcept { return {}; } |
| 21 |
void return_void() const noexcept {} |
| 22 |
void unhandled_exception() const noexcept {} |
| 23 |
}; |
| 24 |
|
| 25 |
struct Awaiter { |
| 26 |
auto await_ready() const noexcept { |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
auto await_suspend(auto self) const noexcept { |
| 31 |
self.resume(); |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
int await_resume() const noexcept { return 42; } |
| 36 |
}; |
| 37 |
|
| 38 |
bool OK = false; |
| 39 |
|
| 40 |
Handle Coro() { |
| 41 |
#ifdef OUTPUT |
| 42 |
std::cout << "1\n"; |
| 43 |
std::cout << co_await Awaiter{} << '\n'; |
| 44 |
#else |
| 45 |
(void) co_await Awaiter{}; |
| 46 |
#endif |
| 47 |
#ifdef OUTPUT |
| 48 |
std::cout << "2\n"; |
| 49 |
#endif |
| 50 |
co_await std::suspend_always{}; |
| 51 |
#ifdef OUTPUT |
| 52 |
std::cout << "3\n"; |
| 53 |
#endif |
| 54 |
OK = true; |
| 55 |
co_return; |
| 56 |
} |
| 57 |
|
| 58 |
int main() { |
| 59 |
Coro(); |
| 60 |
|
| 61 |
if (!OK) |
| 62 |
__builtin_abort (); |
| 63 |
return 0; |
| 64 |
} |