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

Walter Bright newshound2 at digitalmars.com
Thu Mar 18 09:51:27 UTC 2021


On 3/16/2021 3:24 PM, deadalnix wrote:
> Now what happens with move? Well,t he natural way to transpose the described 
> design to move is as follow:
> 1/ move all fields one by one
> 2/ call the move constructor on the result to maintain invariants if need be.
> 
> To me, 2/ furiously sounds like a postblit, but I'm open to the fact that 
> alternatives.

The move constructor is a postblit, but with two arguments and without the 
implicit initial copy.

> I however know for a fact that the proposed magic will open a bag 
> of worm because it doesn't go with the same set of design principle as the rest 
> of the contruction/destruction business.
> 
> For instance, if a field were to be added to a struct, then immediately the move 
> assignment becomes invalid, silently. Worse, if the field itself contains 
> something detructible, now there is something seriously wrong, potentially 
> outside of the struct I'm working with. For instance, if that new field is a 
> smart pointer, then the guarantee provided y the smart pointer are broken, 
> silently.
> 
> Now we might decide, instead, that all field are going to be destroyed at the 
> end of the move assign in the move struct, inc are there are leftovers. But we 
> are now back to the situation where all struct MUST have a null state, or you 
> won't be able to have them as fields of other structs.
> 
> Or we break the guarantees provided by the ctor/dtor mechanism, but in this 
> case, why have it at all? The whole point of ctor and dtor is to ensure that 
> invariant are kept within the program. Let's not break this invariant.
> 

If a field with a move constructor is added to a struct S without one, a default 
move constructor will be created for S that calls the field's move constructor.

If one is added to a struct with an explicit move constructor, it is up to the 
struct programmer to fold it in explicitly, just like he does for explicit 
constructors and destructors.


More information about the Digitalmars-d mailing list