Differing levels of type-inference: Can D do this?

Ali Çehreli acehreli at yahoo.com
Sat Jul 28 07:55:25 PDT 2012


On 07/27/2012 11:49 PM, Chad J wrote:

 > Range r3 = [1,2,3]; // Element type inferred.

If you mean that you wanted a Range!int on the left-hand side, 
unfortunately there is no template type deduction for struct and class 
templates.

On the other hand, there is type deduction for function templates and 
that is the reason for the common approach of providing a convenient 
function along with struct and class templates:

struct Range(T)
{
     this(T[] slice)
     {}
}

Range!T range(T)(T[] args)
{
     return Range!T(args);
}

void main()
{
     auto r = range([1,2,3]);
     assert(typeid(r) == typeid(Range!int));
}

Ali



More information about the Digitalmars-d-learn mailing list