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

Martin Nowak dawg at dawgfoto.de
Sat Nov 12 07:37:20 PST 2011


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.

// 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.


More information about the Digitalmars-d mailing list