Bug 10952 – struct ctor with defaulted parameters should not be allowed
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-09-02T09:59:00Z
Last change time
2013-09-17T12:15:53Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2013-09-02T09:59:30Z
-----
struct S
{
this(int x = 1) { assert(0); } // never throws
}
void main()
{
auto s = S();
}
-----
If we're really never going to support user-provided default ctors (like it is now), then the above should become a compile-time error. That default argument can't be used.
Comment #1 by andrej.mitrovich — 2013-09-02T10:09:41Z
Additionally I'm curious about the class situation:
class C
{
this(int x = 1) { assert(0); }
}
According to the DMDFE, this is *not* a default ctor, and yet, it's the only one generated in the assembly code. What exactly is the terminology for a default constructor (with regards to defaulted parameters)?
It's also interesting that Object.factory returns null if you have any ctor, even one with with a defaulted parameter:
-----
class C
{
this(int x = 1) { }
}
import std.stdio;
void main()
{
auto c = Object.factory("test.C");
assert(c !is null); // fails
}
-----
So we have the situation where:
auto s = S(); // works, but doesn't call the user ctor
auto c = new C(); // works AND calls the user ctor
Object.factory("test.C"); // works only if there's no user-ctor, even one with default parameters
This is an inconsistent mess.
Comment #2 by andrej.mitrovich — 2013-09-17T12:15:53Z
*** This issue has been marked as a duplicate of issue 3438 ***