Explicit default constructor for structs

Jacob Carlborg doob at me.com
Wed Apr 9 10:05:47 PDT 2014


On 2014-04-09 16:59, Benjamin Thaut wrote:
> Just to be clear, I don't want a default constructor for structs that
> gets called implictly by the compiler, like in C++.
>
> Instead I would really love to have a explicit default constructor. E.g.
> it could look like this (alternative a new keyword "explicit" could be
> introduced, but introduction of new keywords is usually avoided if
> possible, AFAIK):
>
> struct Foo
> {
>    this(void)
>    {
>      // do stuff here
>    }
> }
>
> This default constructor would _never_ be called automatically by the
> compiler. (e.g. when a class is constructed that has struct members.) It
> would only be called in cases where the user explictly calls it.
>
> The following lines would call the explicit default constructor
>
> auto foo1 = Foo();
> auto foo2 = new Foo();
> foo1 = Foo(); // calls explicit constructor first, then calls assignment
> operator

What's the advantage over using a static opCall, that it works with "new"?

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list