call member function alias
Jacob Carlborg
doob at me.com
Thu Aug 23 23:47:49 PDT 2012
On 2012-08-23 21:51, Ellery Newcomer wrote:
> if I have a member function alias and corresponding object and
> arguments, is there any way to turn them into a member function call?
>
> e.g.
>
> class X{
> void a();
> }
>
> auto profit(alias fn, T, Args...)(T t, Args args) {
> ???
> }
>
> profit!(X.fn, X)(x);
>
> Constraints are:
>
> 1) must conserve ability to omit default arguments
> 2) if x is a subclass of X which overrides a, must not call overriden a.
>
> I have mutually exclusive solutions for (1) and (2).
>
> .. wait, nevermind. I can probably just wrap the two. It's an
> interesting problem, though, so I guess I'll post it.
>
> For 1) just parse out the parameter list from typeof(&fn).stringof and
> mix it in as profit's arg list, and then just mixin x.a(paramids), but
> that won't counter D's virtual functions
>
> For 2) hack together a delegate
> dg.ptr = x;
> dg.func_ptr = &fn;
>
> but delegates don't support default arguments.
How about this:
import std.stdio;
class Foo
{
auto forward (alias fn, Args...) (Args args)
{
return fn(args);
}
void bar (int a = 3)
{
writeln("bar ", a);
}
}
auto call (alias fn, T, Args...) (T t, Args args)
{
t.forward!(fn)(args);
}
void main ()
{
auto foo = new Foo;
call!(Foo.bar)(foo);
call!(Foo.bar)(foo, 4);
}
Prints:
bar 3
bar 4
Could this work for you?
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list