[Issue 4247] Cannot create default-constructed struct on heap when constructor is defined

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Sep 11 07:01:05 PDT 2010


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


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs at eml.cc


--- Comment #2 from bearophile_hugs at eml.cc 2010-09-11 07:00:37 PDT ---
(In reply to comment #1)
> Also, since when are structs allowed to be allocated on the heap? TDPL
> explicitly states that structs have scoped lifetime.

I double TDPL says this. And if it says so, then it's wrong. TDPL is not a
religious book.


> You cannot override this. See page 244 in TDPL.

The reason given at page 244 is that all type needs a statically defined init.

On the other hand the need to allocate a struct on the heap with no arguments
is real, and the current workarounds look bad for a nice language as D2. This
small program shows that you can create a S2 on the stack:


struct S1 {}
struct S2 {
    this(int) {}
}
void main() {
    S1 s1a;
    auto s1b = new S1;
    S2 s2a; // OK
    auto s2b = new S2; // ERR
}


So I think "new S2;" may just create the same struct that "S2 s2a;" creates,
but allocate it on the heap instead of the stack. I think this is a solution.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list