| 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 |
void use_lambda () |
| 17 |
{ |
| 18 |
int a_copy = 20; |
| 19 |
int a_ref = 10; |
| 20 |
|
| 21 |
auto f = [&, a_copy]() -> coro1 |
| 22 |
{ |
| 23 |
co_yield a_ref + a_copy; |
| 24 |
co_return a_ref + a_copy; |
| 25 |
}; |
| 26 |
|
| 27 |
coro1 A = f (); |
| 28 |
A.handle.resume(); // Initial suspend. |
| 29 |
PRINT ("main: [a_copy = 20, a_ref = 10]"); |
| 30 |
|
| 31 |
int y = A.handle.promise().get_value(); |
| 32 |
if (y != 30) |
| 33 |
{ |
| 34 |
PRINTF ("main: co-yield = %d, should be 30\n", y); |
| 35 |
abort (); |
| 36 |
} |
| 37 |
|
| 38 |
a_copy = 5; |
| 39 |
a_ref = 7; |
| 40 |
|
| 41 |
A.handle.resume(); // from the yield. |
| 42 |
PRINT ("main: [a_copy = 5, a_ref = 7]"); |
| 43 |
|
| 44 |
y = A.handle.promise().get_value(); |
| 45 |
if (y != 27) |
| 46 |
{ |
| 47 |
PRINTF ("main: co-ret = %d, should be 27\n", y); |
| 48 |
abort (); |
| 49 |
} |
| 50 |
PRINT ("use_lambda: about to return"); |
| 51 |
} |
| 52 |
~foo () { PRINT ("foo: DTOR"); } |
| 53 |
}; |
| 54 |
|
| 55 |
int main () |
| 56 |
{ |
| 57 |
foo<int> inst; |
| 58 |
inst.use_lambda(); |
| 59 |
return 0; |
| 60 |
} |