Bug 8118 – Impossible to initialize a member struct without default constructor or assigment
Status
RESOLVED
Resolution
DUPLICATE
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-05-18T17:05:00Z
Last change time
2013-11-28T20:39:33Z
Assigned to
nobody
Creator
wfunction
Comments
Comment #0 by wfunction — 2012-05-18T17:05:19Z
struct S
{
@disable this();
this(int) { }
@disable void opAssign(typeof(this));
}
class Test
{
S s = void; // I *EXPLICITLY* told it not to be initialized, but...
this() { s = S(to!int("1")); }
}
void main() { new Test(); }
Error: function S.opAssign is not callable because it is annotated with @disable
Error: default construction is disabled for type Test
Structs without default constructors are pretty much impossible to use.
(In reply to comment #0)
> struct S
> {
> @disable this();
> this(int) { }
> @disable void opAssign(typeof(this));
> }
>
> class Test
> {
> S s = void; // I *EXPLICITLY* told it not to be initialized, but...
> this() { s = S(to!int("1")); }
> }
>
> void main() { new Test(); }
>
>
> Error: function S.opAssign is not callable because it is annotated with
> @disable
> Error: default construction is disabled for type Test
>
>
>
> Structs without default constructors are pretty much impossible to use.
No bug here you just diabled too much. Undisable opAssign.
It's opAssign that gets called whne a = ... is seen:
this() { s = S(to!int("1")); }
If opAssign is trivial it replaced with bitblit. (that is disable comes first!)
Comment #3 by wfunction — 2012-05-19T12:15:42Z
(In reply to comment #2)
> No bug here you just diabled too much. Undisable opAssign.
>
> It's opAssign that gets called whne a = ... is seen:
> this() { s = S(to!int("1")); }
> If opAssign is trivial it replaced with bitblit. (that is disable comes first!)
Uh, no, it's a bug IMO.
I never asked for an assignment. I want to CONSTRUCT the object manually.
Comment #4 by wfunction — 2012-05-19T12:17:48Z
(In reply to comment #3)
> (In reply to comment #2)
> > No bug here you just diabled too much. Undisable opAssign.
> >
> > It's opAssign that gets called whne a = ... is seen:
> > this() { s = S(to!int("1")); }
> > If opAssign is trivial it replaced with bitblit. (that is disable comes first!)
>
> Uh, no, it's a bug IMO.
>
>
> I never asked for an assignment. I want to CONSTRUCT the object manually.
For example, pretend this is the Scoped struct. I *obviously* wouldn't want to assign anything, but I'd want to construct the object.
The fact that it's **impossible** to call the constructor directly without an assignment getting in the way (as far as I see) is a bug.
Comment #5 by Marco.Leise — 2013-02-15T06:17:42Z
(In reply to comment #4)
> The fact that it's **impossible** to call the constructor directly without an
> assignment getting in the way (as far as I see) is a bug.
I agree with you. I have a struct that is not supposed to be copied. Now I cannot use it as a field in any other struct/class.
In some cases a work-around may be to allow assignments, but check that the receiver is S.init.
Also I tried "= void" first. So it may be the most intuitive to use for the bug fix.
I haven't checked, but it could allow code like this if not currently possible:
S s = void;
if (xyz) {
s = S(3);
} else {
s = S(7);
}
Comment #6 by simen.kjaras — 2013-11-28T12:13:34Z
This seems to be fixed now (code compiles and runs on 2.064).
Comment #7 by k.hara.pg — 2013-11-28T20:39:33Z
(In reply to comment #6)
> This seems to be fixed now (code compiles and runs on 2.064).
The OP case is fixed in 2.064, by fixing issue 9665.
(In reply to comment #5)
> I haven't checked, but it could allow code like this if not currently possible:
>
> S s = void;
> if (xyz) {
> s = S(3);
> } else {
> s = S(7);
> }
This is completely different case. It would need to use std.conv.emplace.
*** This issue has been marked as a duplicate of issue 9665 ***