Avoiding Dangling References
Learn to prevent dangling coroutine references through parameter passing, member functions, lambdas, and guidelines.
The fact that a coroutine can be passed around in our code means that we need to
be very careful about the lifetime of parameters we pass to a coroutine to avoid dangling references. The coroutine frame contains copies of the objects that normally live on the stack, such as local variables and parameters passed to the coroutine. If
a coroutine accepts an argument by reference, the reference is copied, not the object. This means that we can easily end up with dangling references when following the usual guidelines for function parameters; that is, pass objects that are expensive to copy by reference to const
.
Passing parameters to coroutines
The following coroutine uses a reference to a const std::string
:
Get hands-on with 1400+ tech skills courses.