-
Notifications
You must be signed in to change notification settings - Fork 245
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
[QUESTION] Is &$*
a new cpp2 idiom?
#247
Comments
The funny thing is that it looks like grawlix :D |
That makes me think, as there are no references in cpp2, how will we write functions that return a reference in cpp2? Like the [] operator for containers, or do we just start returning pointers in all cases? |
@AbhinavK00, that is an excellent question. It is already covered by the fun: (i : int) -> forward int = {
return i;
}
fun2: (i) -> forward _ = {
return i;
} Generates (skipping boilerplate): [[nodiscard]] auto fun(cpp2::in<int> i) -> int&{ // this is bug should be const int&
return i;
}
[[nodiscard]] auto fun2(auto const& i) -> decltype(auto){
return i;
} |
cppfront syntax concentrates on intentions, not implementation details (reference here is an implementation detail). You can read more here: https://github.com/hsutter/708/blob/main/708.pdf (@hsutter paper about |
Thanks for the answer! Now we just need something similar for lambdas lol |
So with the current state of cppfront, I wonder, would it make sense to have
I guess that's enough speculation for me until @hsutter can clarify his thoughts here. |
@gregmarr just to clarify. |
@filipsajdak I understand, the effect is the same. |
As we discuss references here, I think it is worth mentioning @hsutter sentence he said when I mentioned that
More here: #106 (comment) |
@filipsajdak To answer your original question, yes that's the idiom. I do realize that |
As there is no possibility of passing a reference to the anonymous function (aka. lambda), you need to capture a variable as a pointer. That makes a new idiom emerge from the code:
&$*
- capture variable by pointer (and use as a reference).The following code:
Will generate (skipping boilerplate):
Or maybe I have missed something?
For all that wanders what
i&$*
means (please remember that cppfront uses postfix operators - https://github.com/hsutter/cppfront/wiki/Design-note%3A-Postfix-operators). I will add parentheses to explain:i&
- address ofi
,(i&)$
-$
captures the value on the left (in that case, it is a pointer toi
),( (i&)$ )*
-*
dereference the value captured by$
(in that case, it is a pointer toi
)So, effectively
i&$*
is a reference toi
that is captured by a lambda.The text was updated successfully, but these errors were encountered: