Move Constructors - Converting Lvalues to Rvalues

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Oct 11 18:19:22 UTC 2024


On Monday, September 30, 2024 10:05:16 AM MDT Walter Bright via Digitalmars-d 
wrote:
> 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(): ```

Given the we're now looking at having a separate syntax for move
constructors, I would argue that they should just have to be ref, which
should eliminate the special cases that you're fighting here. Even if an
rvalue is being passed in, it has to be _somewhere_ as a temporary, and
fundamentally, a move constructor needs an lvalue, since it's moving an
object from one location to another, even if the source location is a
temporary one.

- Jonathan M Davis





More information about the Digitalmars-d mailing list