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

          Issue ID: 12918
           Summary: Copying structs on the heap
           Product: D
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: [email protected]
          Reporter: [email protected]

We've recently added universal construction so that stuff like new int(5)
works. However, one thing that still doesn't work is copying structs on the
stack to structs on the heap when allocating them. I think that this code
should compile

struct Foo
{
    int i;
}

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

The compiler knows how to copy Foo, so it should be trivial for it to copy an
existing Foo onto the a newly allocated Foo on the heap, and it would avoid
forcing you to write code like

struct Foo
{
    int i;

    this(int j)
    {
        i = j;
    }

    this(Foo rhs)
    {
        this = rhs;
    }
}

in order to get

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

to work or from forcing you to write code like

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

--

Reply via email to