James
James

Reputation: 97

Creating A Template Wrapper for std::bind

I am trying to create a simple wrapper function for std::bind, which will take a member function.

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

MyClass a = MyClass();
myBindFunction <MyClass, &MyClass::m_Function>( a );

I'm not sure if what I am trying to achieve it possible?

Upvotes: 1

Views: 106

Answers (1)

songyuanyao
songyuanyao

Reputation: 172924

You can make the 2nd template parameter a non-type template parameter, i.e. a member function pointer.

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

LIVE

Upvotes: 1

Related Questions