DIP 1018--The Copy Constructor--Community Review Round 1

RazvanN razvan.nitu1305 at gmail.com
Wed Dec 19 13:40:41 UTC 2018

On Wednesday, 19 December 2018 at 11:53:12 UTC, Kagamin wrote:
> I think const postblit can be done by creating shadow copy of 
> instance fields (but still physically located in the field).
> struct A
> {
>     int b;
>     this(this) immutable
>     {
>         immutable int shadow_b; //reads go here
>         //b++
>         immutable int t=shadow_b;
>         shadow_b.__dtor();
>         b.__ctor(t+1);
>     }
> }
> Type system would treat them as distinct instances, but would 
> destroy shadow copy right before assignment, after which shadow 
> copy's lifetime ends and it goes out of scope completely, which 
> will also invalidate any retained pointers if any.

This makes the blitting useless. Look at it this way: the 
compiler blitts the struct (in this case, `b`), then it makes 
another copy of it (`immutable int t = b) solely for the purpose 
of destroying the field for the former instance (!!!!!!). All 
this copies become useless when using the copy constructor.


More information about the Digitalmars-d mailing list