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

tsbockman thomas.bockman at gmail.com
Mon Mar 15 20:09:15 UTC 2021


On Monday, 15 March 2021 at 05:45:01 UTC, Walter Bright wrote:
> On 3/11/2021 5:40 PM, tsbockman wrote:
>> Is this correct?
>
> 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.

That's not what I was asking about. I understand what a move 
*constructor* does.

Rather, I was trying to figure out, "How can your definition of 
move *assignment* be represented in code?" You tried to answer 
this, my actual question, earlier, but your answer was just as 
vague as the DIP:

 From the DIP:
> After the move is complete, the destructor is called on the
> original contents of the constructed object.

Since the move overwrites the original contents, it's not 
immediately obvious how the destructor can be called on those 
original contents, which are gone, *after* the move.

On Thursday, 11 March 2021 at 08:36:18 UTC, Walter Bright wrote:
> The idea is that the move assignment operation takes care of
> this, and makes it "as if" the move was done, then the
> destruction.

If the algorithm is unclear or missing steps, saying, "You don't 
actually have to do it, you just have to do something 
equivalent," doesn't help. A clear, complete, and logically valid 
algorithm needs to be defined *first*, to determine whether 
another algorithm is equivalent.

I was trying to figure out what your original, general-purpose 
move *assignment* algorithm could be. That's what my code example 
is for.

I found a candidate that seems to work, and to fit your 
description (`this` and `source` are both passed in by reference):

0) Move-construct the `source` into a temporary `newValue`.
1) Clear `source` in such a way that destroying its value would 
be a no-op.
2) Move-construct the original value of the destination `this` 
into a temporary `oldValue`.
3) Move-construct `newValue` into the destination `this`.
4) Destroy `oldValue`.

This is complicated, but everything simpler I tried either 
doesn't match your description, or doesn't pass my tests. 
Runnable example:
     
https://gist.github.com/run-dlang/b789714c01905f091a44ee2666276433

Is this correct?


More information about the Digitalmars-d mailing list