I would like to wrap member functions that conform to the type \'void (ClassType::Function)(ArgType)\' with a templated class. Later, I want to pass an instance of ClassType to
In C++11 you might use lambdas, like:
template
std::function wrapper(void (X::*mfp)(ARG)) { return [=](X *x, ARG arg) { (x->*mfp)(arg); }; }
With VisualC++ (at least as recent as VS2013), use capture by value [=]
when capturing member function pointers (or experience crashes).
Playground:
#include
#include struct A { virtual void a(int i) { std::cout << "A: " << i << std::endl; } }; struct B { virtual void b(int i) { std::cout << "B: " << i << std::endl; } }; template std::function wrapper(void (X::*mfp)(ARG)) { return [=](X *x, ARG arg) { (x->*mfp)(arg); }; } int main() { auto g = wrapper(&B::b); B b; g(&b, 3); auto h = wrapper(&A::a); A a; h(&a, 4); return 0; }