add __rvalue(expression) builtin (#17050)

This commit is contained in:
Walter Bright 2024-12-15 12:13:28 -08:00 committed by GitHub
parent 13775eb2d1
commit a99a3894be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 471 additions and 163 deletions

23
changelog/dmd.rvalue.dd Normal file
View file

@ -0,0 +1,23 @@
Add primary expression of the form `__rvalue(expression)` which causes `expression` to be treated as an rvalue, even if it is an lvalue.
Overloads on `ref`:
```
foo(S s); // selected if `s` is an rvalue
foo(ref S s); // selected if argument `s` is an lvalue
S s;
S bar();
...
foo(s); // selects foo(ref S)
foo(bar()); // selects foo(S)
```
With this change,
```
foo(__rvalue(s)); // selects foo(S)
```
This also applies to constructors and assignments, meaning move constructors and
move assignments are enabled. Moving instead of copying can be much more resource
efficient, as, say, a string can be moved rather than copied/deleted.
A moved object can still be destructed, so take that into account when moving
a field - set it to a benign value that can be destructed.