Passing member-function to template

Maxim Fomin maxim at maxim-fomin.ru
Sat Jan 12 11:06:25 PST 2013


On Saturday, 12 January 2013 at 18:17:47 UTC, Zhenya wrote:
> Hi!
> Tell me please,is there any way to pass member-function to 
> template?

Probably this can help: (you can manually construct a delegate 
combining function and context pointers)

import std.stdio;

template execute(alias func)
{
     void execute(void* ptr)
     {
         void delegate() dg;
		  dg.funcptr = &func;
		  dg.ptr = ptr;
		  dg();
     }
}

struct Foo
{
    void nothing()
    {
		writeln("reached");
    }
}

void main()
{
     Foo f;
     execute!(Foo.nothing)(&f);
}




More information about the Digitalmars-d-learn mailing list