首页 > 解决方案 > 为 std::bind 创建模板包装器

问题描述

我正在尝试为 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 );

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

标签: c++templatesmember-functions

解决方案


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

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

居住


推荐阅读