Possible bug

Steven Schveighoffer schveiguy at yahoo.com
Mon Mar 25 22:40:00 PDT 2013


On Tue, 26 Mar 2013 01:28:03 -0400, Sergei Nosov <sergei.nosov at gmail.com>  
wrote:

> Thank you, guys!
>
> You made the matters clear to me!
>
> It would be an interesting enhancement over C++.
>
> Although, Steven, I didn't quite understand what you're suggesting to  
> use in case of "templated-struct-templated-constructor"
>
> Explicitly specifying struct parameters is ok. Deducing constructor  
> parameters from arguments is also ok. But what if not all of the  
> constructor parameters may be deduced? E.g. one of them is an int?
>
> Would it be a no-no case? or should we merge the lists in one? or should  
> we use two bangs? S!(int, 4)!(byte, 5)(3) ? =)

What you have to do is instantiate the template, then call the constructor:

S!(int, 4).S!(byte, 5)(3)

Note that the way templates work in D, a templated struct is really a  
shortcut for:

template S(T)
{
    struct S { ... }
}

So when you instantiate it, S!(int)(5) is really shorthand for  
S!(int).S(5).

Every template is this way.  The shorthand version calls on the  
"eponymous" member implicitly, that is, the member with the same name as  
the template.  Currently, this only works if there is exactly one member.

For example, a function template is really:

template foo(T)
{
    void foo(T t) {...}
}

So doing:

foo!(int)(1)

is really the same as:

foo!(int).foo(1)

It's all outlined here: http://dlang.org/template.html

search for Implicit Template Properties

-Steve


More information about the Digitalmars-d-learn mailing list