Description
The guideline is clear to disallow reference parameters to coroutines. But the guidance is unclear about pointer params. I think pointer params should be allowed as they make the ownership requirement very explicit and it avoids binding references to temporaries and local variables (unless done explicitly).
Some patterns which are dangerous are:
- wrapper functions unintentionally binding references to local variables.
task<int> coro(const int& a) { co_return a + 1; }
// stack-use-after-return.
task<int> wrapper(int a) { return coro(a); }
- calling coroutines while binding temporaries to them:
task<int> another_coro() {
auto temp = coro(1); // The temporary dies after this statement.
co_return co_await temp;
}
These problems of unintentionally introducing dangling references is solved if the coroutine accepts the parameter as a pointer. Pointer forces L-values. It does not bind to temporaries (these need to named) and does not bind local variables (unless done explicitly).
It would be great if the guidelines could shed some light on these pattern.
More concrete problems which arise due to references include use withstd::function
1, 2