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

Johannes Loher johannes.loher at fg4f.de
Sat Dec 22 08:40:21 UTC 2018


In the section "Copy constructor call vs. blitting", the DIP 
explains how copying is handled when no copy constructor is 
defined:

> When a copy constructor is not defined for a struct, 
> initializations are handled by copying the contents from the 
> memory location of the right-hand side expression to the memory 
> location of the left-hand side expression (i.e. "blitting"):
>
> struct A
> {
>     int[] a;
> }
>
> void main()
> {
>     A a = A([7]);
>     A b = a;                 // mempcy(&b, &a)
>     immutable A c = A([12]);
>     immutable A d = c;       // memcpy(&d, &c)
> }

Then later, in the section "Generating copy constructors", the 
DIP describes under which situations copy constructors are 
generated implicitly:

> A copy constructor is generated implicitly by the compiler for 
> a struct S if:
> 
>     S does not define any copy constructors;
>     S does not have an overlapping field that defines a copy 
> constructor;
>     S defines at least one member that has a copy constructor.
> 
> If all of the restrictions above are met, the following copy 
> constructor is generated:
> 
> this(ref inout(S) src) inout
> {
>     foreach (i, ref inout field; src.tupleof)
>         this.tupleof[i] = field;
> }

Those two statements contradict each other. The first one clearly 
states that whenever no copy constructor is defined, copying is 
done by blitting (without exception).


More information about the Digitalmars-d mailing list