Placement new and @trusted

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Wed Sep 10 12:41:22 UTC 2025


On 11/09/2025 12:29 AM, IchorDev wrote:
> I thought the 'placement new' feature might replace my need for 
> `emplace`, but I don't see a way to mark a placement new as `@trusted` 
> without marking the call to the object's constructor as `@trusted` also?
> 
> ```d
> void main() @safe{
>      X x;
>      //placement new is @system, so we have to wrap it in @trusted:
>      X* xPtr = (() @trusted => new (x) X(500))();
> }
> 
> struct X{
>      void* a;
>      this(int a) @system{
>          //but we don't want to mark THIS as @trusted!
>          this.a = cast(void*)a;
>      }
> }
> ```
> 
> If anyone has any ideas, please let me know.

Your calling a @system function (constructor) from a safe function (main).

Placement new doesn't change the rules around this.


More information about the Digitalmars-d-learn mailing list