member function as template parameter workaround needed

timotheecour thelastmammoth at gmail.com
Thu Sep 13 16:20:48 PDT 2012


As part of a larger system (which converts function parameters to 
a suitable type), I need to solve the following: pass a member 
function to a "run" template function, access its 
ParameterTypeTuple, and actually be able to run the function. 
I've simplified the problem (see below). Is there a way to pass a 
single argument to "run" ?
Both attempts 1 and 2 didn't compile. Attempt 3 compiles but is 
very bulky (need to pass 2 args).

Also I can't pass &a.fun2 as fun2 is not static.



----
class A{
	void fun2(double x){
		writeln("in fun2 with arg:",x);
	}
}
void run(alias fun,T...)(T args){
	writeln(typeid(ParameterTypeTuple!(fun)));
	fun(args);
}
void run2(alias fun,alias fun2,T...)(T args){
	writeln(typeid(ParameterTypeTuple!(fun)));
	fun2(args);
}
void test(){
	auto a=new A;

         //attempt#1
	//run!(a.fun2)(10);//compile error: Error: need 'this' to access 
member fun2

         //attempt#2
         //also tried to just pass x=>a.fun2(x), but then 
ParameterTypeTuple doesn't work with lambdas.


         //attempt#3
	run2!(a.fun2,x=>a.fun2(x))(10);//works but ugly
}
----


More information about the Digitalmars-d-learn mailing list