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

Timon Gehr timon.gehr at gmx.ch
Thu Mar 18 18:10:29 UTC 2021


On 18.03.21 10:51, Walter Bright wrote:
> 
> 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.


This is not the case, you usually don't have to do it explicitly in 
explicit destructors:

---
import std.stdio;

struct T{
     ~this(){ writeln("T destructor called"); }
}

struct S{
     T t;
     ~this(){
         writeln("S destructor called");
         // NOTE: does not explicitly call t.~this()
     }
}

void main(){
     S s;
}
---
S destructor called
T destructor called
---

You may be able to argue that it's not important, but let's not pretend 
that nothing new is going on here. If you don't explicitly _move_ a 
field, the destructor will _not_ be called.

Destructors do the right thing by default, even if you don't update them 
after adding a new field. This is not true for move constructors.


More information about the Digitalmars-d mailing list