Template mixins and struct constructors

Adrian Matoga via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 2 06:50:15 PST 2016


On Wednesday, 2 March 2016 at 14:36:59 UTC, Daniel Kozak wrote:
> OK maybe this one:
>
> template AddField(T) {
>     T b;
>     this(Args...)(T b, auto ref Args args)
>     {
>            this.b = b;
>        this(args);
>     }
>     this(int a) {
>         this.a = a;
>     }
> }
>
> struct Bar {
>     int a;
>     mixin AddField!(string);
> }
>
> unittest {
>     auto bar1 = Bar(5);
>     auto bar2 = Bar("bar", 15);
> }

Then it's limited to structs in which only "int a" is to be 
initialized. Not very useful and the templated ctor is not needed 
now.

struct Baz {
     mixin AddField!string; // fail, no "int a" in Baz.
     ulong d;
     double x;
     string foo;
}


More information about the Digitalmars-d-learn mailing list