You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Cannot value initialise a function argument with equivalent of C++ {} #1090
Hi Neil,
The solution I think might be simplest for this is to use '{}' for default initialized arguments just like c++, and adding it would be pretty straight forward. One approach is to allow brace expressions here:
// If in the future (not now) we decide to allow braced-expressions
// || curr().type() == lexeme::LeftBrace
)
However that approach might not be best one given the comment right above it. I can probably add logic to the parser to grab '{}' only in this specific instance, but I'd like to hear from our BDFL, @hsutter, before going down any route.
if () is changed to lower to {} then wouldn't that imply that explicit constructors should be included in the overload set? i.e.
The following doesn't work in C++ (but should)
struct B {
explicit B(int);
};
void foo(B);
B asdf() {
B _ = auto{1}; // should mean B _ = B{1};
foo({1}); // should mean foo(B{1}); (some might disagree)
foo(auto{1}); // should mean foo(B{1});
if (rand())
return {1}; // should mean return B{1}; (some might disagree)
else
return auto{1}; // should mean return B{1};
}
The following should work in Cpp2:
B: type = {
operator=: (out this, _: i32) = {} // explicit ctor
}
foo: (_: B) = {}
B asdf() {
_: B = (1);
foo((1)); // some might disagree that this should work
foo(:_ = (1));
foo(_(1));
if (rand())
return (1); // some might disagree that this should work
else if (rand())
return :_ = (1);
else
return _(1);
}
Activity
sookach commentedon May 30, 2024
Hi Neil,
The solution I think might be simplest for this is to use '{}' for default initialized arguments just like c++, and adding it would be pretty straight forward. One approach is to allow brace expressions here:
cppfront/source/parse.h
Lines 5715 to 5718 in 130f149
However that approach might not be best one given the comment right above it. I can probably add logic to the parser to grab '{}' only in this specific instance, but I'd like to hear from our BDFL, @hsutter, before going down any route.
Thanks.
hsutter commentedon May 30, 2024
Thanks! In Cpp2 so far,
{}are used only for scopes.Hot take:
()does already mean default construction (e.g.,x : int = ();) so an empty list argument()could be lowered to{}.sookach commentedon May 30, 2024
I like that idea, I'll try it out.
DerpMcDerp commentedon May 31, 2024
if
()is changed to lower to{}then wouldn't that imply thatexplicitconstructors should be included in the overload set? i.e.The following doesn't work in C++ (but should)
The following should work in Cpp2:
Default initialize function argument, closes hsutter#1090 (hsutter#1092)