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

Juan Manuel Cabo juanmanuel.cabo at gmail.com
Mon May 13 21:10:50 PDT 2013


On Tuesday, 14 May 2013 at 03:34:52 UTC, Timothee Cour wrote:
> A)
> The behavior of __ctor (whether or not documented) seems broken 
> / unreliable:
>
> struct A{this(T)(T x){}}
>
> void fun(T){
>   auto a=A.__ctor!(int)(1);
> }
> void main(){
>   auto a=A.__ctor!(int)(1); //ok
>   fun!int(); //Error: type A is not an expression
> }
>
>
> Is that a bug?
>
> B)
> Why not use 'this' instead of '__ctor', and make it documented 
> (and
> reliable, ie work in the above case) ?
> I don't see how that could create ambiguity, and that would 
> solve the
> problem raised in this thread.


I declared fun(T) as fun(T)() with the added parenthesis, and it
worked (tested on dmd 2.062 / ubuntu 64bits).

The following prints "it works" twice:

     import std.stdio;

     struct A {
         this(T)(T x) {
             writeln("it works");
         }
     }

     void fun(T)(){
          auto a=A.__ctor!(int)(1);
     }
     void main(){
          auto a=A.__ctor!(int)(1); //ok
          fun!int(); //ok too
     }

--jm




More information about the Digitalmars-d-learn mailing list