Is "auto t=T();" not the same as "T t;"?

Andrey Zherikov andrey.zherikov at gmail.com
Tue Oct 25 13:51:30 UTC 2022


I have the following types (simplified version of my code):
```d
struct A
{
     int[] i;
}
struct B
{
     A[] a = [A.init];
}
```

This code:
```d
     auto b1 = B.init;
     b1.a[0].i ~= 1;
     b1.a ~= A.init;
     b1.a[0].i ~= 11;
     b1.a[1].i ~= 12;
     b1.writeln;

     auto b2 = B();
     b2.writeln;
```
prints this as expected:
```
B([A([1, 11]), A([12])])
B([A([])])
```

But this code:
```d
     B b1;      // auto b1 = B.init;
     b1.a[0].i ~= 1;
     b1.a ~= A.init;
     b1.a[0].i ~= 11;
     b1.a[1].i ~= 12;
     b1.writeln;

     B b2;     // auto b2 = B();
     b2.writeln;
```
prints this which is not expected:
```
B([A([1, 11]), A([12])])
B([A([1])])
```

Does the second piece of code shows a bug or my expectation is 
not correct (and why if so)?


More information about the Digitalmars-d-learn mailing list