isocpp / isocpp/CppCoreGuidelines
[Proposal] Allow pointer parameters to coroutines (CP.coro section)
Nobody has claimed this yet.
- Dominant language
- CSS
- Stars
- 45.3k
- Forks
- 5.6k
- PR merge metrics
- No merged PRs in 30d
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked CP53 section in CppCoreGuidelines.md and review the wrapper, temporary-binding, and std::function examples in the issue. Determine how the CP.coro guidance should address pointer parameters and the described dangling-reference patterns; the work is done when the guideline clearly documents the resolved recommendation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100