struct and default constructor

Simen Kjærås simen.kjaras at gmail.com
Sun Nov 27 13:24:15 PST 2011


On Sun, 27 Nov 2011 21:19:38 +0100, deadalnix <deadalnix at gmail.com> wrote:

> In addition, here is a workaround :
>
> // Wonderfull !
> @disable this();
>
> // Default constructor workaround.
> this(int dummy = 0) { ... }
>
> But that look very dirty and it feels like working against the language.

I believe your "workaround" will disappoint you, as it simply does not run.

import std.stdio;

struct A {
     int value;
     @disable this();
     this(int dummy = 0) {
         value = 3;
         writeln("Hello from struct default constructor!"); // Never  
happens.
     }
}

void main() {
     A a = A();
     writeln( a.value ); // Writes 0
}

More importantly, this shows a bug in DMD - namely that structs with
@disabled default constructors can be created without a call to any
constructor and with no error message. In fact, if one removes the
constructor with dummy arguments, the program still compiles.

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


More information about the Digitalmars-d mailing list