Separate allocation and construction?
Chris Williams via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jun 5 15:47:13 PDT 2014
On Thursday, 5 June 2014 at 22:25:03 UTC, Adam D. Ruppe wrote:
> On Thursday, 5 June 2014 at 22:22:16 UTC, Chris Williams wrote:
>> If I wanted to allocate memory for a class and then call its
>> constructor as two separate steps, while still having the
>> object be managed by the garbage collector, is there any way
>> to do that?
>
>
> Check out std.conv.emplace
> http://dlang.org/phobos/std_conv.html#emplace
>
> First, allocate the memory block for the class. The size is
> __traits(classInstanceSize, Yourclass). Then slice it:
>
> enum size = __traits(classInstanceSize, YourClass);
> auto memory = GC.malloc(size)[0 .. size];
Why slice? There seems to be a version of emplace that accepts a
pointer, which is what GC.malloc() seems to return.
More information about the Digitalmars-d-learn
mailing list