Static inline field initialization

Moritz Maxeiner via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 22 07:53:21 PDT 2017


On Tuesday, 22 August 2017 at 13:53:05 UTC, Daniel Kozak wrote:
> s/buf/bug/
>
> On Tue, Aug 22, 2017 at 3:52 PM, Daniel Kozak 
> <kozzi11 at gmail.com> wrote:
>
>> On Tue, Aug 22, 2017 at 2:20 PM, Moritz Maxeiner via 
>> Digitalmars-d < digitalmars-d at puremagic.com> wrote:
>>
>>> On Tuesday, 22 August 2017 at 11:50:50 UTC, Jonas Mminnberg 
>>> wrote:
>>>
>>>> ...
>>>
>>>
>>> I agree that it can be confusing if you try to read it with 
>>> C++ semantics [1]; the solution, however, imho is not to 
>>> change D semantics or throw warnings [2], but to refer to the 
>>> D spec [3], which states that static initialization of class 
>>> members is used instead of default initialization (before any 
>>> constructors are run). To me that means a statically 
>>> initialized class field shall have the same value in all 
>>> class instances, i.e. it's not silent sharing, you explicitly 
>>> requested the sharing.
>>>
>>
>> What? D spec does say nothing about sharing, its only speak 
>> about order, nothing else. So it is a buf from my POV.

It doesn't have to and it's not a bug (though considering the 
replies in this thread the wording should be changed to include 
that): D's builtin arrays consist of a pointer and a length; the 
example thus *explicitly* initializes both the pointer and the 
length to the same (static) value for all instances (as per 
spec), sharing them. There is a bug [1] - as others have pointed 
out - that the static array isn't stored in TLS, but in global 
storage, however, but that doesn't apply in this single threaded 
case.

[1] https://issues.dlang.org/show_bug.cgi?id=2947


More information about the Digitalmars-d mailing list