skinny delegates

Steven Schveighoffer schveiguy at gmail.com
Sat Aug 4 12:21:18 UTC 2018


On 8/3/18 1:34 PM, kinke wrote:
> On Friday, 3 August 2018 at 16:46:53 UTC, Jonathan Marler wrote:
>> Maybe you could provide an example or 2 to demonstrate why these would 
>> be requirements...we may have 2 different ideas on how this would be 
>> implemented.
> 
> auto foo(/*mutable*/ int x)
> {
>     return { return ++x; };
> }
> 
> void main()
> {
>      auto dg = foo(42);
>      auto dg_copy = dg;
>      // with the optimization, dg_copy would have its own context
>      // in the ptr field, based on the current state in dg (42)
> 
>      const r1 = dg();
>      const r2 = dg_copy(); // would be 43 with optimization
>      assert(r1 == 43 && r2 == 44);
> }

You don't even need to make a copy to show problems, the context isn't 
passed by reference:

const r1 = dg();
const r2 = dg();

assert(r1 == 43 && r2 == 44); // would fail with optimization.

-Steve


More information about the Digitalmars-d mailing list