www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Who's using structs nested in functions?

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Refer to:

http://www.digitalmars.com/d/2.0/struct.html

and scroll down to the last section, "Nested Structs". A struct defined 
inside a function has a hidden pointer to that function's stack frame 
and therefore can use function's local variables.

Nested classes do a similar trick, but for those there's a bit of 
motivation - you could create a nested class and use it as a sort of 
closure by returning a base class or interface of it.

With nested structs, however, you can't do much. You can pass them to a 
template, but I can't see some solid use cases there. My understanding 
is that nested structs have been implemented for completeness and 
consistency with nested classes.

Any good example of nested struct uses?


Andrei
Oct 22 2009
next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 22 Oct 2009 18:07:15 +0400, Andrei Alexandrescu  
<SeeWebsiteForEmail erdani.org> wrote:

 Refer to:

 http://www.digitalmars.com/d/2.0/struct.html

 and scroll down to the last section, "Nested Structs". A struct defined  
 inside a function has a hidden pointer to that function's stack frame  
 and therefore can use function's local variables.

 Nested classes do a similar trick, but for those there's a bit of  
 motivation - you could create a nested class and use it as a sort of  
 closure by returning a base class or interface of it.

 With nested structs, however, you can't do much. You can pass them to a  
 template, but I can't see some solid use cases there. My understanding  
 is that nested structs have been implemented for completeness and  
 consistency with nested classes.

 Any good example of nested struct uses?


 Andrei

Everything you can do with nested struct is doable with a nested class as well (marking methods as final, using scope to allocate instance on stack etc). I don't think there's anything specific to nested struct. One thing I can think of is that you are using some template that works with structs only, but then you may create a nested class and nest a static struct inside it (not that I would recommend doing so but still) :)
Oct 22 2009
prev sibling next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Andrei Alexandrescu:

 With nested structs, however, you can't do much. You can pass them to a 
 template, but I can't see some solid use cases there. My understanding 
 is that nested structs have been implemented for completeness and 
 consistency with nested classes.
 Any good example of nested struct uses?

I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile
Oct 22 2009
next sibling parent reply Jeremie Pelletier <jeremiep gmail.com> writes:
bearophile wrote:
 Andrei Alexandrescu:
 
 With nested structs, however, you can't do much. You can pass them to a 
 template, but I can't see some solid use cases there. My understanding 
 is that nested structs have been implemented for completeness and 
 consistency with nested classes.
 Any good example of nested struct uses?

I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile

I've had similar uses, some win32 api routines require custom structs like BITMAPINFO, nested structs are neat to declare the struct right before its only usage. However I don't think having a closure for that struct is really needed, nested functions already perform that task very well, and I use those quite often. Jeremie
Oct 22 2009
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Jeremie Pelletier:

 However I don't think having a closure for that struct is really needed, 
 nested functions already perform that task very well, and I use those 
 quite often.

That's why I have said "static nested structs", they are like nested structs, but they don't have the extra field. Bye, bearophile
Oct 22 2009
prev sibling parent reply Fawzi Mohamed <fmohamed mac.com> writes:
On 2009-10-22 16:33:01 +0200, Jeremie Pelletier <jeremiep gmail.com> said:

 bearophile wrote:
 Andrei Alexandrescu:
 
 With nested structs, however, you can't do much. You can pass them to a 
 template, but I can't see some solid use cases there. My understanding 
 is that nested structs have been implemented for completeness and 
 consistency with nested classes.
 Any good example of nested struct uses?

I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile

I've had similar uses, some win32 api routines require custom structs like BITMAPINFO, nested structs are neat to declare the struct right before its only usage. However I don't think having a closure for that struct is really needed, nested functions already perform that task very well, and I use those quite often. Jeremie

I use structs in nested functions as context for parallel recursive loops, actually my code would be nicer if circular referring structs would be allowed in nested functions (at the moment this is not possible, even with forward references). Fawzi
Oct 22 2009
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Fawzi Mohamed wrote:
 On 2009-10-22 16:33:01 +0200, Jeremie Pelletier <jeremiep gmail.com> said:
 
 bearophile wrote:
 Andrei Alexandrescu:

 With nested structs, however, you can't do much. You can pass them 
 to a template, but I can't see some solid use cases there. My 
 understanding is that nested structs have been implemented for 
 completeness and consistency with nested classes.
 Any good example of nested struct uses?

I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile

I've had similar uses, some win32 api routines require custom structs like BITMAPINFO, nested structs are neat to declare the struct right before its only usage. However I don't think having a closure for that struct is really needed, nested functions already perform that task very well, and I use those quite often. Jeremie

I use structs in nested functions as context for parallel recursive loops, actually my code would be nicer if circular referring structs would be allowed in nested functions (at the moment this is not possible, even with forward references). Fawzi

Could you prepend "static" in front of their definition and still have them work? Andrei
Oct 22 2009
parent reply Fawzi Mohamed <fmohamed mac.com> writes:
On 2009-10-22 18:11:01 +0200, Andrei Alexandrescu 
<SeeWebsiteForEmail erdani.org> said:

 Fawzi Mohamed wrote:
 On 2009-10-22 16:33:01 +0200, Jeremie Pelletier <jeremiep gmail.com> said:
 
 bearophile wrote:
 Andrei Alexandrescu:
 
 With nested structs, however, you can't do much. You can pass them to a 
 template, but I can't see some solid use cases there. My understanding 
 is that nested structs have been implemented for completeness and 
 consistency with nested classes.
 Any good example of nested struct uses?

I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile

I've had similar uses, some win32 api routines require custom structs like BITMAPINFO, nested structs are neat to declare the struct right before its only usage. However I don't think having a closure for that struct is really needed, nested functions already perform that task very well, and I use those quite often. Jeremie

I use structs in nested functions as context for parallel recursive loops, actually my code would be nicer if circular referring structs would be allowed in nested functions (at the moment this is not possible, even with forward references). Fawzi

Could you prepend "static" in front of their definition and still have them work? Andrei

Sure, I thought nested structures were always static, and did not have access to the context, as classes do. Fawzi
Oct 23 2009
parent Fawzi Mohamed <fmohamed mac.com> writes:
On 2009-10-23 12:11:20 +0200, Fawzi Mohamed <fmohamed mac.com> said:

 On 2009-10-22 18:11:01 +0200, Andrei Alexandrescu 
 <SeeWebsiteForEmail erdani.org> said:
 
 Fawzi Mohamed wrote:
 On 2009-10-22 16:33:01 +0200, Jeremie Pelletier <jeremiep gmail.com> said:
 
 bearophile wrote:
 Andrei Alexandrescu:
 
 With nested structs, however, you can't do much. You can pass them to a 
 template, but I can't see some solid use cases there. My understanding 
 is that nested structs have been implemented for completeness and 
 consistency with nested classes.
 Any good example of nested struct uses?

I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile

I've had similar uses, some win32 api routines require custom structs like BITMAPINFO, nested structs are neat to declare the struct right before its only usage. However I don't think having a closure for that struct is really needed, nested functions already perform that task very well, and I use those quite often. Jeremie

I use structs in nested functions as context for parallel recursive loops, actually my code would be nicer if circular referring structs would be allowed in nested functions (at the moment this is not possible, even with forward references). Fawzi

Could you prepend "static" in front of their definition and still have them work? Andrei

Sure, I thought nested structures were always static, and did not have access to the context, as classes do. Fawzi

Ok I see that indeed in D2.0 struct have a pointer to the context, no I don't use that. Fawzi
Oct 23 2009
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
bearophile wrote:
 Andrei Alexandrescu:
 
 With nested structs, however, you can't do much. You can pass them to a 
 template, but I can't see some solid use cases there. My understanding 
 is that nested structs have been implemented for completeness and 
 consistency with nested classes.
 Any good example of nested struct uses?

I have used nested static structs sometimes, when I need a struct just inside a function (like the main()) to avoid polluting the outer scope with the struct name. Do you want to remove them from D2? Bye, bearophile

I'm asking about non-static nested structs. Andrei
Oct 22 2009
prev sibling parent Michel Fortin <michel.fortin michelf.com> writes:
On 2009-10-22 10:07:15 -0400, Andrei Alexandrescu 
<SeeWebsiteForEmail erdani.org> said:

 Refer to:
 
 http://www.digitalmars.com/d/2.0/struct.html
 
 and scroll down to the last section, "Nested Structs". A struct defined 
 inside a function has a hidden pointer to that function's stack frame 
 and therefore can use function's local variables.
 
 Nested classes do a similar trick, but for those there's a bit of 
 motivation - you could create a nested class and use it as a sort of 
 closure by returning a base class or interface of it.
 
 With nested structs, however, you can't do much. You can pass them to a 
 template, but I can't see some solid use cases there. My understanding 
 is that nested structs have been implemented for completeness and 
 consistency with nested classes.
 
 Any good example of nested struct uses?

I tried implementing things using nested structs in my mfr.xml module[1] but, for some reasons I don't remember anymore (template issues?), I wasn't too successful and ended up creating anonymous scope classes. All this because of the lack of nested overloaded functions. [1]: see: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=98861 -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Oct 22 2009