Template argument deduction from a function call question

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 1 11:35:42 PDT 2015


On Wednesday, 1 April 2015 at 18:20:41 UTC, Dzugaru wrote:
>> a)
>> isn't this almost, if not exactly, the same as 
>> std.algorithm.reduce?
>>
>> b)
>> you can write nice things like this:
>> auto min = [2,4,1,3,5].aggregate!((a,  b) => a < b ? a : 
>> b)(int.max);
>>
>> c)
>> the deduction failure looks like a bug to me, perhaps there is 
>> a good reason why it can't work in the general case.
>
> a) Had a look at std.algorithm.reduce, looks like this is what 
> I did, but there are 2 overloads inside a template block 
> instead of parameter with a default value (also less strictly 
> typed):
>
> template reduce(fun...) {
> auto reduce(R)(R r)
> auto reduce(S, R)(S seed, R r)
> }
>
> The code there though is mindboggling :)

Yeah it's not the easiest to understand at first glance. The big 
differences are that it works with multiple functions in one 
pass, has it's seed and range the wrong way around for UFCS 
(grrrr!!!) and works with any seed type that can have the result 
of the function(s) assigned to it, which is a lot more flexible 
than insisting on using the element type.*

*One really obvious example is doing a sum in to a larger 
variable so as to avoid overflow.


More information about the Digitalmars-d mailing list