__gshared is "somewhat" transitive, isn't it ?

Steven Schveighoffer schveiguy at gmail.com
Fri May 17 23:03:26 UTC 2024


On Thursday, 16 May 2024 at 17:04:09 UTC, user1234 wrote:
> Given
>
> ```d
> struct S
> {
>     int member;
> }
>
> __gshared S s;
> ```
>
> It's clear that `s.member` is `__gshared` too, right ?
> What does happen for
>
> ```d
> struct S
> {
>     int member;
>     static int globalMember;
> }
>
> __gshared S s;
> ```
>
> Is then `S.globalMember` a TLS variable ? (I'd expect that)

`__gshared` is a storage class. It means, store this thing in the 
global memory segment. `static` storage class means store this 
thing in TLS.

Storage classes are *not* transitive, and they are not type 
constructors. They optionally might apply a type constructor to 
the type (such as the `const` storage class), but not always.

So in this case `typeof(s)` is `S`, not `__gshared S`. `s.member` 
is in the global segment since structs members are placed within 
the struct memory location (in this case, the global memory 
segment).

`globalMember` is placed in TLS because it's storage class is 
`static`, and `static` means, do not store with the instance 
(which for `s` would mean the global memory segment), but rather 
in TLS.

-Steve


More information about the Digitalmars-d-learn mailing list