Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/range-for1.C

raw
1 // PR c++/118574
2 // { dg-do run { target c++20 } }
3 // { dg-additional-options -frange-for-ext-temps }
4
5 #include <coroutine>
6
7 struct A {
8 const char **a = nullptr;
9 int n = 0;
10 void push_back (const char *x) { if (!a) a = new const char *[2]; a[n++] = x; }
11 const char **begin () const { return a; }
12 const char **end () const { return a + n; }
13 ~A () { delete[] a; }
14 };
15
16 struct B {
17 long ns;
18 bool await_ready () const noexcept { return false; }
19 void await_suspend (std::coroutine_handle<> h) const noexcept {
20 volatile int v = 0;
21 while (v < ns)
22 v = v + 1;
23 h.resume ();
24 }
25 void await_resume () const noexcept {}
26 };
27
28 struct C {
29 struct promise_type {
30 const char *value;
31 std::suspend_never initial_suspend () { return {}; }
32 std::suspend_always final_suspend () noexcept { return {}; }
33 void return_value (const char *v) { value = v; }
34 void unhandled_exception () { __builtin_abort (); }
35 C get_return_object () { return C{this}; }
36 };
37 promise_type *p;
38 explicit C (promise_type *p) : p(p) {}
39 const char *get () { return p->value; }
40 };
41
42 A
43 foo ()
44 {
45 A a;
46 a.push_back ("foo");
47 a.push_back ("bar");
48 return a;
49 }
50
51 C
52 bar ()
53 {
54 A ret;
55 for (const auto &item : foo ())
56 {
57 co_await B{200000};
58 ret.push_back (item);
59 }
60 co_return "foobar";
61 }
62
63 int
64 main ()
65 {
66 auto task = bar ();
67 if (__builtin_strcmp (task.get (), "foobar"))
68 __builtin_abort ();
69 }