how to disable all default N-argument constructors for a struct?

Era Scarecrow via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 3 18:02:16 PDT 2017


On Tuesday, 4 July 2017 at 00:46:36 UTC, Timothee Cour wrote:
> How would I disable the following?
>
> ```
> auto a1=A(1);
> auto a2=A(1, "b");
>
> struct A{
>   int a;
>   string b;
>   // @disable default constructors with N(N>=1) arguments
> }
> ```
>
> I'd like to force the user to set fields explicitly, so as to 
> make it more safe to add / move fields

  As soon as a class defines at least one constructor, the 
implicit default constructor is not avaliable anymore. - TDPL pg. 
182


So to answer your question, define a constructor. perhaps:

   this(int _a, string_b) {a=_a; b=_b;}

then:

   auto a1=A(1);      //fails
   auto a2=A(1, "b"); //passes



More information about the Digitalmars-d mailing list