Template type deduced from struct ctor?

Dicebot public at dicebot.lv
Thu Mar 27 10:59:34 PDT 2014


On Thursday, 27 March 2014 at 17:42:24 UTC, Luís Marques wrote:
> Could this be made to work?
>
>     struct S(T)
>     {
>         T x;
>
>         this(T x)
>         {
>             this.x = x;
>         }
>     }
>
>     void main()
>     {
>         int x = 42;
>         auto s = S(x); // fails to deduce T
>     }

struct S(T)
{
     static if (is(T == int))
     {
         this(double x) { }
     }
     static if (is(T == double))
     {
         this(int x) { }
     }
}

auto s = S(42); // should it deduce T, huh, and to what?

I believe it could work but too many complexities arise that need 
to be figured out, especially with overloading in mind so it is 
hardly worth the effort.


More information about the Digitalmars-d mailing list