Placement new and @trusted

IchorDev zxinsworld at gmail.com
Wed Sep 10 12:29:24 UTC 2025


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.


More information about the Digitalmars-d-learn mailing list