Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Allow pointer parameters to coroutines (CP.coro section) #2142

Open
usx95 opened this issue Sep 26, 2023 · 0 comments
Open

[Proposal] Allow pointer parameters to coroutines (CP.coro section) #2142

usx95 opened this issue Sep 26, 2023 · 0 comments

Comments

@usx95
Copy link

usx95 commented Sep 26, 2023

https://github.com/isocpp/CppCoreGuidelines/blob/4b706d9d6d/CppCoreGuidelines.md#cp53-parameters-to-coroutines-should-not-be-passed-by-reference

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:

  1. 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); }

godbolt

  1. 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;
}

godbolt

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

@usx95 usx95 changed the title Guidelines are unclear about pointer parameters to coroutines [Proposal] Allow pointer parameters to coroutines. Sep 26, 2023
@usx95 usx95 changed the title [Proposal] Allow pointer parameters to coroutines. [Proposal] Allow pointer parameters to coroutines (CP.coro section) Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant