Is "auto t=T();" not the same as "T t;"?
Andrey Zherikov
andrey.zherikov at gmail.com
Tue Oct 25 16:02:39 UTC 2022
On Tuesday, 25 October 2022 at 14:53:50 UTC, Adam D Ruppe wrote:
> On Tuesday, 25 October 2022 at 13:51:30 UTC, Andrey Zherikov
> wrote:
>> A[] a = [A.init];
>
> This is a problem - this is referring to a static array
> instance, shared across all copies of B. You almost certainly
> don't want this.
>
> That B.a[0] is the *same object* across different
> default-constructed Bs... unless the optimizer hits it or
> something.
Is it a bad idea to trigger copy on write before modification of
B.a so it behaves as below?
```d
B b1;
b1.a = b1.a.dup; // copy on write
b1.a[0].i ~= '1';
b1.a ~= A.init;
b1.a[0].i ~= '2';
b1.a[1].i ~= '3';
b1.writeln; // B([A("12"), A("3")])
B b2;
b2.writeln; // B([A("")])
```
More information about the Digitalmars-d-learn
mailing list