dynamic array + copy ctor

vit vit at vit.vit
Sun Dec 19 22:29:21 UTC 2021


Hello,
Why is copy ctor in this example not called?

```d
import std.stdio;

struct Foo {
     int i;

     this(int i){
         this.i = i;
         writeln("init: ", i);
     }

     this(ref typeof(this) rhs){
         this.i = rhs.i;
         writeln("copy: ", i);
     }
     ~this() {
         writeln("~dtor:", i);
     }
}

void main(){
     Foo[] foos;

     foos ~= Foo(1);

     while(foos.capacity > foos.length)
         foos ~= Foo(0);

     foos ~= Foo(2);

     import core.memory;
     GC.collect();
}
```
result:
```
init: 1
init: 2
~dtor:1
~dtor:2
~dtor:1
```

First Foo is destructed 2x.



More information about the Digitalmars-d-learn mailing list