Discussion Thread: DIP 1040--Copying, Moving, and Forwarding--Community Review Round 1

Walter Bright newshound2 at digitalmars.com
Mon Mar 15 05:45:01 UTC 2021


On 3/11/2021 5:40 PM, tsbockman wrote:
> I think I finally figure out how to make some sense out of the DIP's 
> description. However, the lowering cannot be expressed clearly with the DIP's 
> syntax, so I will use an alternative notation:
> 
> void moveConstruct(ref S source) nothrow @nogc {
>      if(source.isUnique) {
>          ptr = &internal;
>          internal = source.internal;
>      } else
>          ptr = source.ptr;
> }
> void moveAssign(ref S source) @trusted nothrow @nogc {
>      S oldDest = void;
>      oldDest.moveConstruct(this); // Move the old value to a temporary.
>      moveConstruct(source);
>      // Implicitly destroy the old value.
> }
> 
> Is this correct? If so, the DIP really needs to explain it more clearly - 
> especially if the user is expected to implement some equivalent in the custom 
> move operator himself, rather than the compiler doing it for him.

The idea of the move constructor is the user takes over the task of doing 
whatever it takes to make it work, i.e. handle the destruction.

Think of it this way. You start with two objects, A and B. Moving B into the 
storage occupied by A must result in the destruction of the original object 
occupying A, and whatever is in B after the move is garbage, and what was in B 
is now in A.

Everything follows from that.


More information about the Digitalmars-d mailing list