On Monday, 19 October 2015 at 18:26:45 UTC, Adam D. Ruppe wrote:
On Monday, 19 October 2015 at 18:16:15 UTC, Andrei Alexandrescu
wrote:
Tangentially related: since when we allow field initialization
with new? I was surprised to see that this works:
Since CTFE started supporting it... this might actually be an
unintentional feature.
Since the initializer is in a static context, the right hand
side gets CTFE'd, which means that actually points to an array
in the data segment... the *same* array in the data segment for
all initializations (the pointer is just blitted over with the
rest of init), which might be a bit surprising.
I've seen a lot of people do this with classes not realizing it
makes a static instance!
Yeah. It makes sense when you're dealing with an immutable
object/arrays (and possibly const, assuming that it wasn't
initialized with a mutable variable that was directly initialized
via new), but it really makes no sense for mutable objects/arrays
- not unless you have some weird case where you want each
instance of your object to be able to share that member variable
initially and then possibly stop sharing later. But given the
high probability that someone is going to do this and shoot
themselves in the foot, I think that we'd be a lot better off if
we disallowed it.
- Jonathan M Davis