The Lambda Coroutine Fiasco
https://github.com/scylladb/seastar/blob/master/doc/lambda-coroutine-fiasco.mdIt's amazing C++23's "deducing this" could solve the lambda coroutine issue, and eliminate the previous C++ voodoo.
44
Upvotes
View all comments
Show parent comment
5
u/SirClueless Dec 19 '25
I disagree. This has nothing to do with capturing by value or reference, both are broken. This is a wholly new problem. The idea that putting
co_awaitinside your lambda implicitly means that its return value holds a reference to the lambda itself and thus will dangle if the lambda is destroyed is a new and subtle footgun.Concrete example:
This code is pretty much always lifetime-safe. There are some things the caller can do that end up holding onto references to the lambda's captures in a broken way like
foo([x] { return std::ref(x); }), but this is a kind of "obvious error" that almost no one makes.But if you call this with a coroutine it is super easy to shoot yourself in the foot:
Oops,
cbwas destroyed whenfoo()returned, and then when the coroutine was resumed,xdangles.