Second Draft: Placement New Expression

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Tue Nov 19 05:17:20 UTC 2024


On 19/11/2024 6:05 PM, Richard (Rikki) Andrew Cattermole wrote:
>  > To allocate storage with an allocator function such as malloc(), a 
> simple template can be used:
> 
> Why not support calling the allocator with the size needed?
> 
> ```d
> struct Allocator {
>      void[] allocate(size_t);
> }
> 
> Allocator a;
> 
> new (allocator) S;
> new (malloc) S;
> ```
> 
> DONE.

There are three reasons that motivate me for allocator support:

1. It would be @safe.
2. There is no trait like ``__traits(allocateSize, 
T)``/``__traits(allocateSize, T, arrayCount)``. Handle overflow 
correctly and "just work" regardless of type, i.e. class vs struct vs 
array has a very different way of getting allocation size.
3. Needing wrapper functions for this is genuinely silly. As a result I 
don't understand why it hasn't been adopted already.



More information about the dip.development mailing list