static array of structs clarification questions

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 13 08:50:01 PST 2016


On Saturday, 13 February 2016 at 14:53:39 UTC, ZombineDev wrote:
> On Saturday, 13 February 2016 at 10:22:36 UTC, Marc Schütz 
> wrote:
>> On Friday, 12 February 2016 at 21:56:09 UTC, Steven 
>> Schveighoffer wrote:
>>> That's odd. I think anonymous probably has the answer (they 
>>> are context pointers), but I'm also surprised they are null, 
>>> they shouldn't be.
>>
>> In this example, `void foo()` doesn't access any outer 
>> variables, so there's no need for a context to be created.
>
> Yes, but the compiler will create a context regardless of this. 
> See also this issue:
> https://issues.dlang.org/show_bug.cgi?id=15343

It adds a hidden member, but it doesn't actually allocate a 
context, therefore the member is null:

     auto foo() @nogc {
         int j;
         struct Foo {
             int i;
             void info() { i += 5; }
         }
         return Foo();
     }

Replace `i` by `j`, and it no longer compiles, because then it 
really allocates a context.

As for your bug report: I believe the documentation specifies the 
current behaviour somewhere, but I cannot find it now. Nested 
structs always have a context pointer, except if they don't have 
methods (for layout compatibility with C). I think this is 
necessary to avoid "paradoxa" like the following:

     int j;

     struct Foo {
         int i;
         void info() {
             static if(Foo.sizeof == 4)
                 j += 5;
             else
                 i += 5;
         }
     }


More information about the Digitalmars-d-learn mailing list