C++17

Mathias Lang via Digitalmars-d digitalmars-d at puremagic.com
Wed Jan 27 00:48:20 PST 2016


2016-01-27 8:10 GMT+01:00 rsw0x via Digitalmars-d <
digitalmars-d at puremagic.com>:

> I have to manually create my own functors to be able to capture variables
> by value in lambdas so that they're usable in @nogc. How C++98.
> Alias template parameters are again massive hidden abusers of the GC.


For delegate you can use `scope delegate`, which is stack-allocated.
Using a sink-based approach like `toString` that specific point is quite
easily solved.
`scope` safety being unimplemented, it's also quite easy to abuse if you
don't have control over the function decl:

```
import core.stdc.stdio;
void main () @nogc
{
    string foobar = "Value";
    scope sink = (int u) { printf("%s: %d\n", foobar.ptr, u); };
    foo(sink);
}

void foo (void delegate (int) @nogc sink) @nogc
{
    sink(42);
}
```
Alternatively, you can make foo's parameter `scope` and use the literal
inline, it's recognized by @nogc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20160127/c17e034d/attachment.html>


More information about the Digitalmars-d mailing list