this() not executing code on structs

Don nospam at nospam.com
Fri Oct 23 07:15:00 PDT 2009


Bartosz Milewski wrote:
> Andrei Alexandrescu Wrote:
> 
>>     this() { myCount = count++; }       // ERROR
> 
> It's worse than that. Try this:
> 
> struct foo {
>        this(int dummy = 0) { writeln("Default constructor");}
> }
> 
> foo x = foo();
> 
> Nothing gets printed. If default constructors are disallowed, so should constructors with all parameters defaulted.

Ouch.
It's because it's interpreting foo() as a struct literal.
If a struct has any constructors, struct literals should be disabled.



More information about the Digitalmars-d mailing list