Template argument deduction from a function call question

Dzugaru via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 1 11:27:36 PDT 2015


On Wednesday, 1 April 2015 at 18:13:15 UTC, Ali Çehreli wrote:
> On 04/01/2015 10:57 AM, Dzugaru wrote:
>
> > ElementType!S aggregate(alias func, S)(S list, ElementType!S
> accum =
> > ElementType!S.init)
> > if(is(typeof(func(accum, accum)) == typeof(accum))) {
> [...]
> > }
>
> I can't explain exactly why that doesn't work.
>
> However, I've discovered a number of times that reducing the 
> dependency between template parameters helps. Instead of using 
> ElementType!S in the parameter list, introduce a third one (E), 
> which you check in the template constraint:
>
> ElementType!S aggregate(alias func, S, E)(S list, E accum = 
> E.init)
> if(is (E == ElementType!S) &&                   // <-- ADDED
>     is(typeof(func(accum, accum)) == typeof(accum))) {
>         // ...
> }
>
> Now it works.
>
> Ali

This code does work when you provide second (non-default) 
argument to function, and doesn't if you do not (no way it can 
deduce E solely from checks I assume).

My version, in constract, works when you do not provide second 
argument and doesn't if you do.


More information about the Digitalmars-d mailing list