简体   繁体   中英

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?

You can make the 2nd template parameter a non-type template parameter , ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM