I have a method that is invoked a lot in my code with this signature:
void foo (std::function<void(int)> func, int a) {
func(a);
}
I can easily pass a function like this to the method:
static void bar(int a);
foo(bar, 42)
Also, if I have a class instance, I can pass my the function pointer to my instance roughly like this ugly snippet:
class Baz {
bar(int a) {...}
callFoo() {
foo(std::bind(&Baz::bar, this, _1);
}
}
However, in my app, I often need to invoke foo in places where I hold a pointer to an object that has a valid member function which could serve as an argument to foo. I don't really want to give every class that happens to hold a valid pointer a method that invokes 'foo'. In fact, I would prefer that those classes don't even know about foo.
I want a method that looks like this:
template<class T>
static void callFooSugar(T* ClassPointer, [What goes here?] ClassMethod);
So I could do this:
Baz MyBaz;
callFooSugar(&MyBaz, bar);
Is there a way to write callFooSugar without resorting to macros? I am compiling with C++11.
Aucun commentaire:
Enregistrer un commentaire