Template type deduced from struct ctor?

Indigo Brown Indigo at Brown.edu
Thu Mar 27 10:52:33 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
>     }

Sure, it *could* work but I doubt it ever actually will.

Instead, why not pass the type directly?

auto s = S!(typeof(x))(x);?

I know it's a little more work and not very pretty but it should 
solve the problem.

Or, if your constructors are simple enough, maybe something like 
this would work

struct S(alias X)
{
     this() { this.x = X; }
}

...

auto s = S!(x);





More information about the Digitalmars-d mailing list