Placement new and @trusted
IchorDev
zxinsworld at gmail.com
Thu Sep 11 08:06:17 UTC 2025
On Thursday, 11 September 2025 at 04:19:02 UTC, Paul Backus wrote:
> The problem is not really with placement new, it's with
> constructors. Constructors are allowed to mutate immutable
> objects (under the assumption that they are initializing a
> newly-created object). If you call a constructor twice on the
> same immutable object, which is possible with placement new, it
> can result in undefined behavior.
But that's the thing: all I want is to construct objects into
*freshly-allocated*, *uninitialised memory*; so my desired
use-case has a safe interface and can therefore be marked
`@trusted`. However the constructor is a wildcard, so I want to
leave that part to attribute inference.
Do you think it'd be worth submitting an enhancement issue to add
something simple like this?
```d
new @trusted (buffer) S(500); //we trust that the buffer is safe
to use, but not S's constructor.
```
The weird placement new syntax means that this looks a bit goofy,
but it's better than the feature being essentially
dead-on-arrival for the *one thing* I'd ever want it for.
More information about the Digitalmars-d-learn
mailing list