且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将指向任何成员函数的指针作为类模板参数传递

更新时间:2022-01-14 00:39:48

我认为类模板是不可能的,因为你必须明确指定成员函数类型.

I think it's impossible for class template, because you have to specify the member function type explicitly.

模板函数可以帮助您进行参数推导:

Template function could help you lot with the argument deduction:

template<typename T, typename M, typename... Args>
void invoke (T& Object, M Method, Args&&... A)
{
    (void)(Object.*Method)(std::forward<Args>(A)...);
}

然后

invoke(myObj, &Object::MyMethod, 10);

现场直播