Copy a struct and its context

Yuxuan Shui via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 13 13:11:11 PDT 2016


On Tuesday, 13 September 2016 at 20:00:40 UTC, Steven 
Schveighoffer wrote:
> On 9/13/16 3:42 PM, Yuxuan Shui wrote:
>> [...]
>
> There's nothing in the language to prevent this optimization.
>
>> [...]
>
> Again, could be clearer. But the fact that both the function 
> and the struct affect the same data kind of dictates it needs 
> to be a reference.
>
>> [...]
>
> Not familiar with C++ lambda. You can always "specify" how to 
> capture the data by directly declaring it:
>
> auto foo()
> {
>     int x;
>     static struct S
>     {
>         int x;
>     }
>     return S(x);
> }

It just feels a bit tedious to do something manually while the 
compiler have enough information to do it for me.

>
> In D, if you have a closure, it's going to be heap allocated. 
> Just the way it is. If you don't want that, you have to avoid 
> them.
>
> -Steve



More information about the Digitalmars-d-learn mailing list