Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
January 12, 2013 Passing member-function to template | ||||
---|---|---|---|---|
| ||||
Hi! Tell me please,is there any way to pass member-function to template? I need something like that: template execute(alias obj,alias mfun) { void execute() { obj.mfun(); } } struct Foo { void nothing() { } } void main() { Foo f; execute!(f,Foo.nothing)(); } I could pass mfun by string,but I think it's a little bit ugly. |
January 12, 2013 Re: Passing member-function to template | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhenya | 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);
}
|
January 12, 2013 Re: Passing member-function to template | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Saturday, 12 January 2013 at 19:06:27 UTC, Maxim Fomin wrote:
> 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);
> }
Thank you,it really can help me.
But I would like to handle not only member-function,but global function too(by UFCS) if it's possible.
|
January 12, 2013 Re: Passing member-function to template | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhenya | On Saturday, 12 January 2013 at 19:16:02 UTC, Zhenya wrote:
> But I would like to handle not only member-function,but global function too(by UFCS) if it's possible.
Global functions rewritten as UFCS functions are not delegates, they are still functions, so they can be handled just as other functions. In such case it is enough to supply null to void* parameter.
|
January 12, 2013 Re: Passing member-function to template | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Saturday, 12 January 2013 at 19:24:04 UTC, Maxim Fomin wrote:
> On Saturday, 12 January 2013 at 19:16:02 UTC, Zhenya wrote:
>> But I would like to handle not only member-function,but global function too(by UFCS) if it's possible.
>
> Global functions rewritten as UFCS functions are not delegates, they are still functions, so they can be handled just as other functions. In such case it is enough to supply null to void* parameter.
Understood,thank you.
|
Copyright © 1999-2021 by the D Language Foundation