----------------8<----------------8<----------------
import std.datetime;
import std.stdio;
struct A{
auto fun(A a){ return 0; }
}
void bench(alias fun)(string msg, uint n = 1_000_000){
auto b = benchmark!fun(n);
writefln(" %s %s ms", msg, b[0].to!("msecs", int));
}
unittest{
A a, b;
void test1(){
auto r = a.fun(b);
}
bench!( {auto r = a.fun(b);} )("Does Not work");
bench!(test1)("Works");
}
void main(){ }
---------------->8---------------->8----------------
DMD 2.058, GNU/Linux 64-bit
/usr/include/d/dmd/phobos/std/datetime.d(30986): Error: safe function
'benchmark' cannot call system delegate '__lambda1'
t1.d(11): Error: template instance
t1.__unittest2.benchmark!(__lambda1) error instantiating
t1.d(23): instantiated from here: bench!(delegate @system void()
{
int r = a.fun(b);
}
)
t1.d(23): Error: template instance t1.__unittest2.bench!(delegate @system void()
{
int r = a.fun(b);
}
) error instantiating
Comment #1 by k.hara.pg — 2012-02-25T21:24:01Z
(In reply to comment #0)
> ----------------8<----------------8<----------------
> import std.datetime;
> import std.stdio;
>
> struct A{
>
> auto fun(A a){ return 0; }
> }
>
> void bench(alias fun)(string msg, uint n = 1_000_000){
>
> auto b = benchmark!fun(n);
> writefln(" %s %s ms", msg, b[0].to!("msecs", int));
> }
>
> unittest{
>
> A a, b;
>
> void test1(){
> auto r = a.fun(b);
> }
>
> bench!( {auto r = a.fun(b);} )("Does Not work");
> bench!(test1)("Works");
> }
>
> void main(){ }
> ---------------->8---------------->8----------------
>
> DMD 2.058, GNU/Linux 64-bit
>
> /usr/include/d/dmd/phobos/std/datetime.d(30986): Error: safe function
> 'benchmark' cannot call system delegate '__lambda1'
> t1.d(11): Error: template instance
> t1.__unittest2.benchmark!(__lambda1) error instantiating
> t1.d(23): instantiated from here: bench!(delegate @system void()
> {
> int r = a.fun(b);
> }
> )
> t1.d(23): Error: template instance t1.__unittest2.bench!(delegate @system
> void()
> {
> int r = a.fun(b);
> }
> ) error instantiating
A.fun is not marked as @safe, so {auto r = a.fun(b);} inferred as @system.
Then delegate inference has no problem.
Additionally, std.datetime.benchmark requires that its arguments fun are @safe. I don't know why that.
Comment #2 by k.hara.pg — 2013-02-19T00:42:10Z
(In reply to comment #1)
> A.fun is not marked as @safe, so {auto r = a.fun(b);} inferred as @system.
> Then delegate inference has no problem.
>
> Additionally, std.datetime.benchmark requires that its arguments fun are @safe.
> I don't know why that.
Hmm, my comment was not correct, and probably a compiler bug had been there.
But, it works with 2.062. Compiler bug seems already be fixed.