Emplace vs closures

Lodovico Giaretta via Digitalmars-d digitalmars-d at puremagic.com
Mon Sep 19 05:24:18 PDT 2016


On Monday, 19 September 2016 at 11:45:25 UTC, ag0aep6g wrote:
> Note that it would also segfault with `auto ps = S.init;`, and 
> for the same reason: missing context pointer.

Oh. I didn't thought about that. This means that in the following 
example, the initialization of `s` is more than a simple call to 
`S.init`. I was under the impression that in D `S.init` should 
represent a valid state for whatever type `S`.

void main()
{
     int x = 42;
     struct S
     {
         auto getX() { return x; }
     }

     S s; // this line does not simply call S.init,
          // but also creates a closure and puts it inside s.
}

> There is a difference, though: You're copying an existing 
> object here, including the context pointer. So maybe we could 
> disallow the variant above that writes the .init value, and 
> still allow the copying variant.

Yeah, I will try that.


More information about the Digitalmars-d mailing list