What should happen here?
Kagamin
spam at here.lot
Fri Sep 24 11:03:59 UTC 2021
On Tuesday, 21 September 2021 at 12:02:05 UTC, Steven
Schveighoffer wrote:
> I just thought of a possible easy and effective way to ensure
> the thing isn't collected early:
>
> ```d
> struct Pin(T)
> {
> T t;
> @nogc nothrow pure @safe ~this() {}
> alias t this;
> }
>
> ...
> // usage
> auto c = Pin!C(new C); // now it needs to be held until the
> scope ends
> ```
Another way:
```
struct Pin(T)
{
T t;
~this()
{
import core.volatile;
volatileLoad(cast(uint*)&t);
}
}
// usage
auto c = Pin!C(new C);
```
More information about the Digitalmars-d
mailing list