no-arg constructor for structs (again)

bearophile bearophileHUGS at lycos.com
Wed Sep 19 15:59:43 PDT 2012


monarch_dodra:

> struct S
> {
>     int* p;
> }
> struct S2
> {
>     int* p;
>     this(int){};
> }
>
> void main()
> {
>     S a;
>     S* pa;
>     //auto b  = S;
>     auto pb = new S;
>     auto c  = S.init;
>     //auto pc = ???
>     auto d  = S();
>     auto pd = new S();
>     auto e  = S2(5);
>     auto pe = new S2(5);
> }

Tangential to your discussion: this needs to be allowed, because 
this removes one unnecessary special case/limit, avoiding the 
need to write some stupid boilerplate constructor code (I have 
written tons of those):

struct Foo {
     int x, y;
}
void main() {
     auto f = new Foo(5, 10);
}


The currently usable workaround is silly in a language like D:

struct Foo {
     int x, y;
}
void main() {
     auto f = new Foo;
     *f = Foo(5, 10);
}

Bye,
bearophile


More information about the Digitalmars-d mailing list