how is this array subtyping inside struct (bug?) possible?

mw mingwu at gmail.com
Sun Aug 9 18:45:07 UTC 2020


Hi,

I want to share an array among a number of structs, with 
subtyping, I tried this, and find some strange behavior:

```
class SharedArray(T) {
   public T[] array;
   alias array this;  // subtyping
}

alias Filenames = SharedArray!(string);

struct S {
   Filenames fns;
   void alloc() {
     fns = new Filenames();
   }
}

void main(string[] args) {
   S s0;
   s0.alloc();
   s0.fns ~= "abc";

   foreach (i; 0..3) {
     S* s1 = new S();
     *s1 = s0;   //  copy the value from s0 to *s1
     writeln(s0.fns);
   }
}
```

program output:
```
["abc"]
[]
[]
```

why s0.fns changed after copy the value from s0 to *s1?

Is this a bug?


More information about the Digitalmars-d mailing list