Bug 10186 – default construction is disabled even if default ctor declared
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-05-27T14:13:55Z
Last change time
2023-02-27T14:47:33Z
Keywords
pull, rejects-valid
Assigned to
No Owner
Creator
Henning Pohl
Comments
Comment #0 by henning — 2013-05-27T14:13:55Z
struct S {
@disable this();
this(int i) {
}
}
class C {
this() {
s = S(1);
}
S s;
}
void main() {
auto c = new C;
}
-----
main.d(17): Error: default construction is disabled for type C
-----
(In reply to comment #1)
> https://github.com/D-Programming-Language/dmd/pull/2087
The fix is incomplete.
struct S
{
@disable this();
this(int i) {}
}
class C
{
S s; // move definition before the ctor
this() { s = S(1); }
}
void main()
{
auto c = new C; // line 16
}
test.d(16): Error: default construction is disabled for type C
Comment #4 by github-bugzilla — 2013-06-01T13:53:10Z
Comment #5 by verylonglogin.reg — 2013-06-02T23:18:19Z
What about `this(int = 0)` class constructor?
Comment #6 by samukha — 2013-10-08T10:59:20Z
And, with v2.064-devel-64d739f, C.s is still assigned from a temporary object instead of being constructed directly. That defeats the purpose of "@disable this", because assignment assumes that the target object has been properly constructed. And, in the presence of "@disable this", "set to init" does not qualify for "properly constructed".
(In reply to comment #6)
> And, with v2.064-devel-64d739f, C.s is still assigned from a temporary object
> instead of being constructed directly. That defeats the purpose of "@disable
> this", because assignment assumes that the target object has been properly
> constructed. And, in the presence of "@disable this", "set to init" does not
> qualify for "properly constructed".
The issue has been already filed in bug 9665.
Comment #9 by k.hara.pg — 2013-11-28T21:07:21Z
*** Issue 7121 has been marked as a duplicate of this issue. ***
Comment #10 by verylonglogin.reg — 2015-01-07T14:07:23Z
Also one even can't create an array of class refereces:
---
struct S
{
@disable this();
this(int i) {}
}
class C
{
S s;
this() { s = S(1); }
}
void main()
{
auto c = new C[1]; // line 15
}
---
main.d(15): Error: default construction is disabled for type main.C
---
Note:
This case is easily workaroundable e.g. setting `length` property.
Comment #11 by razvan.nitu1305 — 2023-02-27T14:47:33Z