Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/class-05-lambda-capture-copy-local.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } }
3
4 // template parm in a class
5
6 #include "../coro.h"
7
8 // boiler-plate for tests of codegen
9 #define USE_AWAIT_TRANSFORM
10 #include "../coro1-ret-int-yield-int.h"
11
12 template <typename T>
13 class foo
14 {
15 public:
16 auto get_lam (int parm)
17 {
18 int local = 3;
19 auto l = [=](T y) -> coro1
20 {
21 T x = y;
22 co_return co_await x + y + local;
23 };
24 return l;
25 }
26 };
27
28 int main ()
29 {
30 foo<int> inst {};
31 auto ll = inst.get_lam (10);
32
33 PRINT ("main: create coro1");
34 int arg = 17; // avoid a dangling reference
35 coro1 x = ll (arg);
36 if (x.handle.done())
37 abort();
38
39 x.handle.resume();
40 PRINT ("main: after resume (initial suspend)");
41
42 x.handle.resume();
43 PRINT ("main: after resume (co_await)");
44
45 /* Now we should have the co_returned value. */
46 int y = x.handle.promise().get_value();
47 if ( y != 37 )
48 {
49 PRINTF ("main: wrong result (%d).", y);
50 abort ();
51 }
52
53 if (!x.handle.done())
54 {
55 PRINT ("main: apparently not done...");
56 abort ();
57 }
58 PRINT ("main: returning");
59 return 0;
60 }