__gshared as part of alias

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Jan 17 07:53:24 UTC 2018


On Wednesday, January 17, 2018 07:30:30 Nicholas Wilson via Digitalmars-d-
learn wrote:
> On Wednesday, 17 January 2018 at 02:37:07 UTC, Steven
>
> Schveighoffer wrote:
> > On 1/11/18 11:25 PM, Nicholas Wilson wrote:
> >> Is there a way to make __gshared part of an alias?
> >
> > No, __gshared is a storage class, not a type constructor, so it
> > has no meaning as part of a type:
> >
> > __gshared int x;
> > int y;
> >
> > static assert(is(typeof(x) == typeof(y)));
> >
> >> as in
> >>
> >> enum AddrSpace : uint
> >> {
> >>
> >>      Private  = 0,
> >>      Global   = 1,
> >>      Shared   = 2,
> >>      Constant = 3,
> >>      Generic  = 4,
> >>
> >> }
> >>
> >> struct Variable(AddrSpace as, T)
> >> {
> >>
> >>      T val;
> >>      alias val this;
> >>
> >> }
> >> alias Global(T)   = __gshared Variable!(AddrSpace.Global,   T);
> >
> > dmd famously doesn't complain about attributes that do nothing,
> > as in this case.
> >
> > -Steve
>
> I kluged this into place in LDC
> https://github.com/ldc-developers/ldc/pull/2509/commits/7cf6f417f95a5bffa4
> b18f2d8b132ca8f0e900d3#diff-33d7d08455db33f1182e3936fd2ba3f9R896

I don't know what you're doing, and maybe __gshared is the appropriate
solution for what you're trying to do, but in general, if you're not trying
to bind to a C global variable, you should be using shared, and using
__gshared is risking bugs precisely because it is not considered part of the
type and does not prevent you from using it a thread-local context. The
compiler will treat it as thread-local, risking subtle bugs that shared
would catch.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list