www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static struct definition

reply "monarch_dodra" <monarchdodra gmail.com> writes:
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
   {
   }
}
Aug 27 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Aug 27 2012
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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?
Aug 27 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Aug 27 2012
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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?
Aug 28 2012
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Tue, 28 Aug 2012 12:10:47 +0200, monarch_dodra <monarchdodra gmail.co=
m>  =

wrote:

  From TDPL: 7.18:

 "Unlike classes nested within classes, nested structs and nested class=
es =
 within
 structs don=E2=80=99t contain any hidden member outer=E2=80=94there 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
Aug 28 2012
parent "monarch_dodra" <monarchdodra gmail.com> writes:
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.
Aug 28 2012