Function template literals - or "just" parameter type inference?

Timon Gehr timon.gehr at gmx.ch
Wed Nov 20 06:57:30 PST 2013


On 11/20/2013 09:23 AM, Kenji Hara wrote:
> 2013/11/20 Jakob Ovrum <jakobovrum at gmail.com <mailto:jakobovrum at gmail.com>>
>
>
>         I was not sure that the fun!int is legitimate usage.
>
>
>     I also ask this question. I think it may prove useful, and code in
>     the wild might be relying on it.
>
>
> One of the known issue is, that the specified type parameters may not
> match exactly to the function parameters position.
>
> You can make a partially specialized lambda like follows.
>
> X!((int a, b) => a + b)
>
> Then, inside X(alias fun), fun!long means instantiate (int a, long b){
> return a + b; }.
> And of course, fun!(int, int) will be invalid.
> ...

The requirements are supposed to be captured by the template constraint.

> So, specifying type parameter for the template lambda will make the
> template code unreliable.

Typically one will be able to pass any arbitrary function template in 
there too, and for those there can be _arbitrary_ functional 
dependencies between the template arguments and the parameter types. 
Relying on any correspondence is invalid in general.

>
> I think it is not good feature in general.
>
> Kenji Hara

The entities declared as 'foo' and 'bar' below are similar enough that 
it does not make sense to treat them differently in this aspect.

auto foo(T)(int x, T y){ return y; }
// ---
template ID(alias a){ alias ID=a; }
alias bar=ID!((int x,y)=>y);

pragma(msg, foo!int(1,2));
pragma(msg, bar!int(1,2));



More information about the Digitalmars-d mailing list