static struct definition

bearophile bearophileHUGS at lycos.com
Mon Aug 27 09:39:33 PDT 2012


monarch_dodra:
> What exactly does it mean when you put static in front of a 
> struct _definition_ (not instance) ?
>
> EG:
>
> static struct S
> {
>   static struct SS
>   {
>   }
> }
>
> As opposed to
>
> struct S
> {
>   struct SS
>   {
>   }
> }

For the outer struct S I think it means nothing, it's just the 
stupid DMD compiler that accepts random qualifiers and attributes.

void foo() {
   static void bar() {}
   struct struct Spam {}
   struct Baz {}
   Baz baz;
   // static assert(Baz.sizeof == size_t.sizeof);
}

For the inner structs like Spam it's supposed to mean something. 
Just like static inner functions like bar() can't refer to 
variables from the enclosing function, static structs like Spam 
are like global structs, it's just their name that is visible 
inside the enclosing function foo. Truly inner structs like Baz 
should have a hidden pointer field that points to the enclosing 
struct. In practice I don't remember if this feature is already 
present in the D front-end.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list