[Issue 1860] New: Heap construction of structs with args doesn't work

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Feb 21 13:28:49 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1860

           Summary: Heap construction of structs with args doesn't work
           Product: D
           Version: 1.027
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: wbaxter at gmail.com


Heap construction of structs using arguments doesn't work.  It doesn't matter
if there's a static opCall explicitly defined or not.

-----
struct NoOpCalls
{
    int i;
    float x;
}
struct WithOpCall
{
    static WithOpCall opCall(int _i, float _x) {
        WithOpCall R; with(R) {
            i = _i;
            x = _x;
        } return R;
    }

    int i;
    float x;
}


void main()
{
    auto stacka = NoOpCalls(2, 5.0); // ok
    auto stackb = WithOpCall(2, 5.0); // ok

    auto heapa = new NoOpCalls(2, 5.0); // error no constructor
    auto heapb = new WithOpCall(2, 5.0); // error no constructor
}
----

This seems like an oversight rather than a deliberate design decision.

You can work around by "double initializing":

   auto heapa = new NoOpCalls; *heapa = NoOpCalls(2,5.0);

But that seems unnecessarily repetitive.

Anyway, this is yet another reason why structs should have real constructors,
as opposed to the static opCall hack.


-- 



More information about the Digitalmars-d-bugs mailing list