Thread overview
static struct definition
Aug 27, 2012
monarch_dodra
Aug 27, 2012
bearophile
Aug 27, 2012
Andrej Mitrovic
Aug 27, 2012
bearophile
Aug 28, 2012
monarch_dodra
Aug 28, 2012
Simen Kjaeraas
Aug 28, 2012
monarch_dodra
August 27, 2012
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
  {
  }
}
August 27, 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
August 27, 2012
On 8/27/12, bearophile <bearophileHUGS@lycos.com> wrote:
>Truly inner structs like Baz
> should have a hidden pointer field that points to the enclosing struct.

Isn't this limited to just classes?
August 27, 2012
Andrej Mitrovic:

> Isn't this limited to just classes?

See the last section of this page:
http://dlang.org/struct.html

>Nested Structs: A nested struct is a struct that is declared inside the scope of a function or a templated struct that has aliases to local functions as a template argument. Nested structs have member functions. It has access to the context of its enclosing scope (via an added hidden field).<

Bye,
bearophile

August 28, 2012
On Monday, 27 August 2012 at 18:53:23 UTC, bearophile wrote:
> Andrej Mitrovic:
>
>> Isn't this limited to just classes?
>
> See the last section of this page:
> http://dlang.org/struct.html
>
>>Nested Structs: A nested struct is a struct that is declared inside the scope of a function or a templated struct that has aliases to local functions as a template argument. Nested structs have member functions. It has access to the context of its enclosing scope (via an added hidden field).<
>
> Bye,
> bearophile

From TDPL: 7.18:

"Unlike classes nested within classes, nested structs and nested classes within
structs don’t contain any hidden member outer—there is no special code generated.
The main design goal of nesting such types is to enforce the desired access control."

I suppose this has become obsolete then?
   ...Or is it the other way around?
August 28, 2012
On Tue, 28 Aug 2012 12:10:47 +0200, monarch_dodra <monarchdodra@gmail.com> wrote:

>  From TDPL: 7.18:
>
> "Unlike classes nested within classes, nested structs and nested classes within
> structs don’t contain any hidden member outer—there is no special code generated.
> The main design goal of nesting such types is to enforce the desired access control."
>
> I suppose this has become obsolete then?
>     ...Or is it the other way around?

TDPL trumps most everything else, I believe.

-- 
Simen
August 28, 2012
On Tuesday, 28 August 2012 at 11:06:51 UTC, Simen Kjaeraas wrote:
> On Tue, 28 Aug 2012 12:10:47 +0200, monarch_dodra <monarchdodra@gmail.com> wrote:
>
>> From TDPL: 7.18:
>>
>> "Unlike classes nested within classes, nested structs and nested classes within
>> structs don’t contain any hidden member outer—there is no special code generated.
>> The main design goal of nesting such types is to enforce the desired access control."
>>
>> I suppose this has become obsolete then?
>>    ...Or is it the other way around?
>
> TDPL trumps most everything else, I believe.

Well, at the same time, the reason I asked is because Andrei told me to declare a sub-struct as static in one of my pull requests. Given he's the author of TDPL...

Anyways, I'll try to bring him into the conversation from.