Static struct?

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Apr 24 21:34:43 UTC 2019


On Wed, Apr 24, 2019 at 09:12:10PM +0000, JN via Digitalmars-d-learn wrote:
> I noticed a construct I haven't seen before in D when reading the
> description for automem - https://github.com/atilaneves/automem
> 
>  static struct Point {
>         int x;
>         int y;
>     }
> 
> 
> What does "static" do in this case? How does it different to a normal
> struct?

In module-global scope, nothing.

In function/aggregate scope, means the struct won't have the hidden
context pointer to its lexical container, so you won't be able to access
variables from the containing scope.  OTOH, it also makes it possible to
instantiate the struct outside that scope (since otherwise it's not
possible to construct it with the proper context pointer).

So ironically, 'static' in this case actually means "normal", as opposed
to a struct that carries a hidden context pointer and has restrictions
as to where/how it can be used.


T

-- 
Frank disagreement binds closer than feigned agreement.


More information about the Digitalmars-d-learn mailing list