Placement new and @trusted

Paul Backus snarwin at gmail.com
Thu Sep 11 17:16:35 UTC 2025


On Thursday, 11 September 2025 at 11:47:40 UTC, Dennis wrote:
> On Wednesday, 10 September 2025 at 12:29:24 UTC, IchorDev wrote:
>> If anyone has any ideas, please let me know.
>
> The trick that's used in druntime is putting the part that 
> still needs to be checked for attributes inside an `if (false)` 
> block, for example:
>
> https://github.com/dlang/dmd/blob/c81714b9bc7626e1d235fb2d6279dc73d47c9174/druntime/src/core/lifetime.d#L1981

You can also use `static if` and a no-op `@system` function:

```d
@system pure nothrow @nogc
pragma(inline, true)
void inferSystem() {}

T* example(T)()
{
     import std.traits: isSafe;

     static if (!isSafe!(() { new T(); })
         inferSystem();

     void[] memory = new void[](T.sizeof);
     return (() @trusted => new (memory) T())();
}
```


More information about the Digitalmars-d-learn mailing list