On 09/12/2011 09:38 AM, Max Samukha wrote:
On 09/11/2011 08:57 PM, Walter Bright wrote:
On 9/11/2011 9:08 AM, Max Samukha wrote:
This test case
struct S
{
@disable this();
this(int x)
{
}
}
class C
{
S s;
this()
{
s = S(42);
}
}
void main()
{
auto c = new C;
}
yields Error: default construction is disabled for type C
Is it a bug?
No, it's a feature!
That's sad. The question should rather have been: what do I do to use
member structs that have default constructors disabled? Initially I
thought that the compiler would treat the first assignment in the
constructor specially as initializer. The error message "Error:
constructor test.C.this field s must be initialized in constructor"
suggested that. I was wrong.
You were right, it does. You just cannot default construct C if S cannot
be default constructed. This works:
struct S {
@disable this();
this(int x) {
}
}
class C {
S s;
this(int) {
s = S(42); // comment out this line and it does not compile
}
}
void main() {
auto c = new C(0);
}
And that implies that the 'feature' of infectious default constructor
disabling for classes is worthless. But it makes perfect sense for structs.