[Issue 12918] Copying-constructing structs onto the heap

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Jul 23 11:47:04 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=12918

Thayne <astrothayne at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |astrothayne at gmail.com

--- Comment #4 from Thayne <astrothayne at gmail.com> ---
A workaround for this is to allocate a Foo then assign it to the struct.

struct Foo
{
    int i;
}

void main()
{
    auto f = Foo(5);
    auto g = new Foo;
    *g = f;
}

However, if the Foo struct has the default constructor disabled (@disable
this();) then this doesn't work, even if postblit isn't disabled. The only way
I can think of to get this to work in general would be something like this:

auto f= Foo(5);
auto g = cast(Foo*) (new ubyte[Foo.sizeof]).ptr;
*g = f;

which is pretty awkward.

--


More information about the Digitalmars-d-bugs mailing list