https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95111
--- Comment #19 from Iain Sandoe <iains at gcc dot gnu.org> --- (In reply to Ville Voutilainen from comment #17) > (In reply to Ville Voutilainen from comment #16) > > (In reply to Iain Sandoe from comment #14) > > > (In reply to Ville Voutilainen from comment #12) > > > The idea of bringing the lambda's captures into the coro frame was what I > > > originally implemented. Richard pointed out that this is wrong when the > > > lambda is mutable (see > > > gcc/testsuite/g++.dg/coroutines/torture/lambda-10-mutable.C) > > > > > > so if one has > > > > > > auto X = [...] () -> some_coro<xxx> {}; > > > > > > X must exist for the duration of the lambda coro [it was pointed out by > > > Lewis that really this is only the same as saying that if you have a class > > > with a member function lambda, the instance of that class has to persist > > > for > > > the duration of the coro]. > > > > Ah. So the work-around for this problem is to copy the capture to a local > > variable, and co_return that; then the local variable is in the coro-state. > > Right? > > That is, instead of writing > > [x] {co_return x;} > > write > > [x] {auto xx = x; co_return xx;} hmm I'm not sure this is sufficient; if the initial suspend is suspend-always, and the closure object goes away before the initial resume, then xx will be initialised with garbage.