Move Constructors - Converting Lvalues to Rvalues
Walter Bright
newshound2 at digitalmars.com
Mon Sep 30 16:05:16 UTC 2024
I've been implementing move constructors. They are working, at least on my test
cases so far. They are distinguished by a copy constructor takes its argument by
ref, and a move constructor by value:
```
struct S
{
this(ref S); // copy constructor
this(S); // move constructor
}
```
So far, so good. Consider:
```
void phone(S s)
{
S t = s; // copy constructor
}
```
But what if we want to initialize `t` via a move constructor? Somehow, `s` has
to be converted from an lvalue to an rvalue. This is done via the function rvalue():
```
S rvalue(ref S s) { return s; }
S t = rvalue(s);
```
Unfortunately, rvalue() gives us this:
```
S __copytmp = 0;
this(&__copytmp,&s); // copy construction of s
*__H1D1 = __copytmp;
return __H1D1;
```
which is correct, but not what we want, which is:
```
return &s;
```
and also not pass an extra hidden parameter for the return value, aka __H1D1.
I have thought of several ways to do this, none of which seem particularly
attractive as they are all not expressible in conventional D. I won't say what
they are in order to not bias anyone.
So, does anyone have any brilliant ideas for how to make the compiler treat an
lvalue as an rvalue?
P.S. C++ uses the std::move function to do it:
https://learn.microsoft.com/en-us/cpp/standard-library/utility-functions?view=msvc-170#move
which relies on rvalue references:
https://learn.microsoft.com/en-us/cpp/cpp/rvalue-reference-declarator-amp-amp?view=msvc-170
which is a major feature which I prefer to avoid.
More information about the Digitalmars-d
mailing list