Template struct literals should infer template parameters

Ali Çehreli acehreli at yahoo.com
Sun Aug 7 08:02:53 PDT 2011


On Sat, 06 Aug 2011 20:19:06 +0000, Sean Eskapp wrote:

> If I have something like
> 
>     struct S(T)
>     {
>         T data;
>     }
> 
> 
> I can't make a struct literal with
> 
>     int mystuff;
>     someFunc(S(mystuff));

It has been the same in C++. The reason is that the syntax above would 
conflict with and is reserved for a templated constructor.

> I have to use
> 
>     int mystuff;
>     someFunc(S!(int)(mystuff));
> 
> Why is this?

To allow flexibility by not tieing the struct's template parameters to 
its constructor's template parameters. Deduction actually works for the 
constructor:

struct S(T)
{
    T data;

    this(U)(U)
    {}
}

void someFunc(T)(T)
{}

void main()
{
    double d;
    someFunc(S!int(d));
}

The constructor's template parameter is being deduced as 'double' on the 
last line.

Ali


More information about the Digitalmars-d mailing list