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

[SUGGESTION] Allow perfect return capturing r: forward _ = func() #1332

Open
MaxSagebaum opened this issue Nov 4, 2024 · 2 comments
Open

Comments

@MaxSagebaum
Copy link
Contributor

MaxSagebaum commented Nov 4, 2024

With decltype(auto) r = func(); the return from a function can be perfectly captured.

With the change in 0.8, we have now -> foward _ = {...} for perfect return type forwarding. We could enable the same for perfect capturing. E.g.:

  r: forward _ = func(x);
  use_r(r);

Edit1:
As @JohelEGP mentioned forward r: _ = func(x); would be more consistent.
Edit1_end

Why is this required?
If use_r is declared as use_r: (inout r) = {...} then the direct return forwarding use_r(func(x)) is not always possible. If func(x) returns a value it can not be bound to a reference.

My use case:
I kind of need this in the regular expression implementation. My matching context is always captured as an inout value. I do not want to have copies of this value. For the lookbehind implementation and nested versions of lookbehind and lookforward, I need to convert the context into itself or a reverse context. So one is a reference and one is a new object. For these cases I need decltype(auto) r = ....

I circumvented the issue currently by relaxing the inout ctx to forward ctx on the generated functions. But then I had to add a hack to prevent forwarding of ctx by adding a _ = ctx; at the end of the function.

I tried to find the location in cppfront where this is implemented, but could not find it. With a hint I would be filling to create a PR.

@JohelEGP
Copy link
Contributor

JohelEGP commented Nov 4, 2024

FWIW, we already have that syntax you mentioned.

I just noticed that we also don't have decltype(auto) for variables.
The best approximation are forward: _ parameters: https://cpp2.godbolt.org/z/MKsocP6bb.

-- #921 (comment)

So maybe rather than
r: foward _ = func(x);,
it should be
forward r := func(x);
for consistency.

@MaxSagebaum MaxSagebaum changed the title [SUGGESTION] Allow perfect return capturing r: foward _ = func() [SUGGESTION] Allow perfect return capturing r: forward _ = func() Nov 4, 2024
@MaxSagebaum
Copy link
Contributor Author

You mean v2 in your code example that is marked as does not work?

If so, that is what I am suggesting. But you are right forward r: _ = func(x); would be more consistent. I added this to the initial post. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants