Static constructors in structs.

BBasile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 30 14:29:21 PDT 2015


On Friday, 30 October 2015 at 20:58:37 UTC, anonymous wrote:
> On 30.10.2015 21:23, TheFlyingFiddle wrote:
>> Is this intended to work?
>>
>> struct A
>> {
>>     __gshared static this()
>>     {
>>        //Add some reflection info to some global stuff.
>>        addReflectionInfo!(typeof(this));
>>     }
>> }
>>
>> I just noticed this works in 2.069, is this intended?
>
> static constructors are supposed to work, yes.
>
> The description is on the class page: 
> http://dlang.org/class.html#static-constructor
>
> __gshared doesn't do anything there, though. Use `shared static 
> this` instead, if you want the constructor to run only once per 
> process, and not once per thread.

__gshared is mostly usefull on fields (eg public uint a) because 
it prevents a data to be put on the TLS, which in certain case 
reduces the perfs up to 30%. The byte code using a global 
variable that's not __gshared can be incredibly slower !


More information about the Digitalmars-d-learn mailing list