Why I can't pass to datetime.benchmark function with parameters?

Anonymouse via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 23 11:11:53 PDT 2016


On Saturday, 23 April 2016 at 15:11:13 UTC, Suliman wrote:
> Working:
> void main()
> {
>  auto r = benchmark!(foo)(1);
> }
>
[...]
>
> Do not working:
> void main()
> {
>  auto r = benchmark!(foo())(1);
> }
>
[...]
>
>>Error: expression foo() is void and has no value
>
> Do not working:
> void main()
> {
>  auto r = benchmark!(4)(1);
> }
>
[...]
>
>
> Why I second two variants do not compile?

The first template argument to std.datetime.benchmark is the 
function you want to benchmark, which you have specified 
correctly in your first example. benchmark!(function)(times).

In the second one you're first calling foo() to pass *the value 
it returns* as template argument to benchmark. Since it returns 
void, you're essentially calling benchmark!(void)(1), not what 
you want. If foo() had returned a function pointer it would 
likely have worked.

In the third example you're providing an integer as the function 
to benchmark, and foo is never mentioned.

The documentation explicitly says that the function must not take 
any parameters. I'm not sure why, but that's how it was designed. 
That said, you can easily work around it by making an 
intermediate function/delegate that passes the arguments you 
want, or a lambda. Unsure whether this incurs any performance 
hits, if it doesn't inline.

You can make a wrapper like in http://dpaste.dzfl.pl/c856b9be53e9 
but the extra argument has to be known during compilation.


More information about the Digitalmars-d-learn mailing list