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

RazvanN razvan.nitu1305 at gmail.com
Thu Dec 20 14:50:39 UTC 2018


On Thursday, 20 December 2018 at 14:41:49 UTC, Kagamin wrote:
> On Wednesday, 19 December 2018 at 13:40:41 UTC, RazvanN wrote:
>> [...]
>
> A copy constructor is very verbose though, imagine a copy 
> constructor for a struct with 30 fields, where it copies 29 
> fields and sets the 30th to null.

For that you can use metaprogramming:

static foreach (i, ref field; src.tupleof)
     this.tupleof[i] = field;

this.field_of_choice = null;

>  Postblit is observation of pattern that copy constructor 
> usually changes only a small part of the struct. In this 
> synthetic example copying wouldn't make much difference:
> for this code:
> ---
> struct A
> {
>     int b;
>     void inc()
>     {
>         const int shadow_b=b;
>         //b++
>         const int t=shadow_b;
>         b=t+1;
>     }
> }
>
> A f(ref A a)
> {
>     A b=a;
>     b.inc();
>     return b;
> }
> ---
> `ldc -Os` generates this code for function f:
> ---
> 	movl	(%rdi), %eax
> 	addl	$1, %eax
> 	retq
> ---



More information about the Digitalmars-d mailing list