This question stems from this comment: Lambda lifetime explanation for C++20 coroutines
regarding this example:
auto foo() -> folly::coro::Task<int> {
auto task = []() -> folly::coro::Task<int> {
co_return 1;
}();
return task;
}
So the question is whether executing the coroutine returned by foo
would result in UB.
"Calling" a member function (after the object's lifetime ended) is UB: http://eel.is/c++draft/basic.life#6.2
...any pointer that represents the address of the storage location where the object will be or was located may be used but only in limited ways. [...] The program has undefined behavior if:
[...]
-- the pointer is used to access a non-static data member or call a non-static member function of the object, or
However, in this example:
- the
()
operator of the lambda is called while the lifetime of the lambda is still valid - It is then suspended,
- then the lambda is destroyed,
- and then the member function (operator
()
) is resumed at some point afterwards.
Is this resumption considered undefined behavior?