how to instantiate explicitly template parameters in struct A(T1){this(T2)(){...}}

Steven Schveighoffer schveiguy at yahoo.com
Mon May 13 15:06:50 PDT 2013


On Mon, 13 May 2013 16:27:44 -0400, Timothee Cour  
<thelastmammoth at gmail.com> wrote:

> While writing DIP40, I came upon the following question:
> how to instantiate explicitly template parameters for both the
> class/struct AND the constructor?
> for example: struct A(T1){this(T2)(){...}}  ? (T2 could be used
> somehow inside the ctor body for example)
> // auto a=A!double!int(); // CT error
> The example is a bit contrived but there could be less contrived ones
> where both A and ctor need explicit instantiations.

Since you can't call the ctor directly [1], this isn't possible in any  
case, even without a templated struct.

What you CAN do is specify a constructing function:

struct A(T1) {
   static A make(T2)(){...}
}

auto a = A!double.make!int();

-Steve

==================
[1] This does work, but is not documented/official AFAIK:

struct S
{
     this(T)(int i) {}
}
void main()
{
     S s;
     s.__ctor!double(1);
}


More information about the Digitalmars-d-learn mailing list