I want to make a template class whose constructor takes an object pointer and a pointer to one of the object's methods. The template class must accept a method with any arguments, so I thought the method's type should be a template parameter. I'd also prefer to accept a method with any return type, but it would be OK to constrain it to returning void. The code below doesn't compile. What's the right syntax?
template <typename Obj, typename Method>
class Foo
{
public:
Foo(Obj *obj, Obj::*Method method)
:mObj(obj), mMethod(method)
{}
void callMethod()
{
mObj->mMethod();
}
private:
Obj* mObj;
Obj::*Method mMethod;
};
class Bar
{
public:
// I want it to work no matter what arguments this method takes.
void method() {}
};
Bar bar;
Foo <Bar, void(Bar::*)()> foo(&bar, &Bar::method);
I get this error on the Foo constructor:
error C2059: syntax error: '<tag>::*'
My previous question on this topic was marked as duplicate, but the examples cited specify the exact type of method that can be passed, and I need it to be generic.