Is "auto t=T();" not the same as "T t;"?
Adam D Ruppe
destructionator at gmail.com
Tue Oct 25 14:53:50 UTC 2022
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.
But just don't do this. Only basic values and immutable strings
are good to initialize this way. With the array or class objects,
you're liable to get some shared thing.
If you change this to be initialized in a constructor (which will
require an argument in D) or factory function, you'll get far
more consistent behavior as each instance will have its own array.
As for why B() and B.init are different here... i don't know,
probably some subtly of the compiler's implementation.
More information about the Digitalmars-d-learn
mailing list