Who's using structs nested in functions?

Fawzi Mohamed fmohamed at mac.com
Fri Oct 23 03:11:20 PDT 2009


On 2009-10-22 18:11:01 +0200, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> Fawzi Mohamed wrote:
>> On 2009-10-22 16:33:01 +0200, Jeremie Pelletier <jeremiep at 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




More information about the Digitalmars-d mailing list