Move Constructors - Converting Lvalues to Rvalues
Walter Bright
newshound2 at digitalmars.com
Thu Oct 3 15:35:36 UTC 2024
What if you have:
```
void foo(S);
void foo(ref S);
void foo(@move S);
```
? The overloading rules are already much more complex than I intended.
```
S foo(S s)=>s;
```
> There is actually no reason for this to make a copy, and the compiler would
in principle be able to figure it out.
I did consider it (actually `S foo(ref S s)=>s;`), but didn't like it because it
would require major changes in multiple places in some already complex code. It
was too high risk of introducing all kinds of unexpected interactions and
problems. And, in the end, __lvalue(s) does the same thing and is fairly simple.
I'm also currently struggling with:
```
this(S);
this(ref S);
```
and then trying to get "is it copyable" to work. The trouble is that a move
constructor not only has an S for an rvalue, that S must also be the same as the
struct name. This cannot be determined by the parser, it needs semantic
analysis. This means that when the constructor is searched for, it has to
exclude matching on a move constructor, that cannot be detected before semantic
analysis.
The solution will likely be adding another parameter to the name lookup. Sigh,
more slowdowns and complexity.
The __rvalue thing, on the other hand, requires (I think) very little disruption
to the compiler and language.
More information about the Digitalmars-d
mailing list