lambda function with "capture by value"

Stefan Koch via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 5 11:19:05 PDT 2017


On Saturday, 5 August 2017 at 18:17:49 UTC, Simon Bürger wrote:
> If a lambda function uses a local variable, that variable is 
> captured using a hidden this-pointer. But this capturing is 
> always by reference. Example:
>
>     int i = 1;
>     auto dg = (){ writefln("%s", i); };
>     i = 2;
>     dg(); // prints '2'
>
> Is there a way to make the delegate "capture by value" so that 
> the call prints '1'?
>
> Note that in C++, both variants are available using
>    [&]() { printf("%d", i); }
> and
>    [=]() { printf("%d", i); }
> respectively.

No currently there is not.


More information about the Digitalmars-d-learn mailing list