Templates problem

rikki cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 7 01:32:45 PDT 2016


On 07/09/2016 2:38 AM, Russel Winder via Digitalmars-d-learn wrote:
> The code fragment:
>
> 	const results = benchmark!(run_mean, run_mode, run_stdDev)(1);
> 	const times = map!((TickDuration t) { return (to!Duration(t)).total!"seconds"; })(results);
>
> seems entirely reasonable to me. However rdmd 20160627 begs to differ:
>
> run_checks.d(20): Error: template std.algorithm.iteration.map!(function (TickDuration t) => to(t).total()).map cannot deduce function from argument types !()(const(TickDuration[3])), candidates are:
> /usr/include/dmd/phobos/std/algorithm/iteration.d(450):        std.algorithm.iteration.map!(function (TickDuration t) => to(t).total()).map(Range)(Range r) if (isInputRange!(Unqual!Range))
> Failed: ["dmd", "-v", "-o-", "run_checks.d", "-I."]
>
> and I have no idea just now why it is complaining, nor what to do to
> fix it.

Ok, I have it mostly compiling.

void run_mean() {}
void run_mode() {}
void run_stdDev() {}

void main() {
	import std.datetime;
	import std.algorithm : map;
	import std.conv;
	import std.array;
	
	const results = benchmark!(run_mean, run_mode, run_stdDev)(1);
	const times = map!((TickDuration t) { return 
(to!Duration(t)).total!"seconds"; })(results[]);
	
	import std.stdio;
	foreach(v; times) {
		writeln(v);	
	}
}

However times really can't be const. Since times is an input range which 
gets modified as part of the iterations.

So what exactly do you want times to be? If you want an array .array 
from std.algorithm it which can be const. Either way there is something 
I'm missing as to why const is so important here.


More information about the Digitalmars-d-learn mailing list