Copy a struct on the heap and get the pointer to it.

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Nov 12 08:53:15 PST 2011


On 11/12/11 9:37 AM, Martin Nowak wrote:
> On Sat, 12 Nov 2011 16:17:29 +0100, Adam D. Ruppe
> <destructionator at gmail.com> wrote:
>
>> The way I'd do it is:
>>
>> S s;
>>
>> auto heap = new S;
>>
>> *heap = s;
>>
>>
>> that should work.
>
> auto ps = std.conv.emplace(new S, s);
>
> Avoids the hacky array aliasing.

That constructs S two times.

> // slightly more efficient
> auto ps2 = emplace(cast(S*)(new ubyte[](S.sizeof)).ptr, s);
>
> You will need to define a copy constructor for immutable fields.

That should work but it's rather complicated.


Andrei


More information about the Digitalmars-d mailing list