为 std::bind 创建模板包装器

Creating A Template Wrapper for std::bind

本文关键字:包装 建模 创建 std bind      更新时间:2023-10-16

我正在尝试为 std::bind 创建一个简单的包装函数,它将采用一个成员函数。

template<typename T, typename F>
void myBindFunction(T &t)
{
   std::bind(T::F, t );
}
MyClass a = MyClass();
myBindFunction <MyClass, &MyClass::m_Function>( a );

不确定我想要实现的目标是否可行?

您可以将第二个模板参数设置为非类型模板参数,即成员函数指针。

template<typename T, void(T::*F)()>
void myBindFunction(T &t)
{
   std::bind(F, t); // bind the member function pointer with the object t
}