xxxxxxxxxx
import std.stdio;
class Aclass
{
int i;
void foo() { writeln("called ", i); }
}
struct Bstruct
{
int i;
void foo() { writeln("called ", i); }
}
template callFoo(T)
{
alias Dun = void delegate();
void callFoo(T t)
{
Dun fun;
fun.funcptr = &T.foo;
fun.ptr = cast(void*)(&t);
Dun gun;
gun = &t.foo;
writeln(fun.ptr, " (fun.ptr of " ~ T.stringof ~ ")");
writeln(gun.ptr, " (gun.ptr of " ~ T.stringof ~ ")");
writeln(&t, " (Address of instance (context))");
fun();
gun();
}
}
void main()
{
auto a = new Aclass();
a.i = 5;
auto b = Bstruct();
b.i = 7;
writeln("---- Doesn't work ----");
callFoo(a);
writeln("\n---- Works ----");
callFoo(b);
}