Summary: Cannot create default-constructed struct on heap when
                    constructor is defined
           Product: D
           Version: 2.041
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD

--- Comment #0 from Steven Schveighoffer <> 2010-05-28 
15:55:49 PDT ---
If I want a struct on the heap, I do this:

struct S

auto s = new S;

But if I give S a constructor:

struct S
  this(int n) {}

auto s = new S;

dmd complains:
Error: constructor S.this (int n) is not callable using argument types ()

So I do this:

struct S
  this() {}
  this(int n) {}

but dmd now complains:
Error: constructor S.this default constructor not allowed for structs

So the auto s = new S is no longer usable.  However, I can easily declare a
struct S on the stack, or create a new array of S.  In fact, you can do:

auto s = (new S[1])[0];

and achieve the desired effect, but this is oh so ugly.

Bottom line, if I can construct a struct on the stack, I should be able to
identically construct it on the heap.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to