C++ bind member function with variadic template -
my example below uses variadic templates register function inside class. registering single function works class member functions? tried std::bind expects placeholders not option because don't know number of arguments. there simple way doing in c++11 or forced implement register function every amount of arguments?
template<typename treturn, typename... targs> class func { std::function<treturn (targs...)> fn; template<typename tfunction, typename tobject> bool register(tfunction f, tobject obj){ } bool register(std::function<treturn (targs...)> f){ fn = f; return true; } }
create lambda function required signature , construct std::function
object it.
template<typename treturn, typename... targs> class func { std::function<treturn (targs...)> fn; template<typename tfunction, typename tobject> bool register(tfunction f, tobject obj){ fn = [obj](targs... args){return (obj.*f)(args...);}; return true; } bool register(std::function<treturn (targs...)> f){ fn = f; return true; } }
(note : member function pointers need used corresponding object, object reference or pointer object of appropriate class. if tobject
value-like, member function call syntax (obj.*f)
. if tobject
pointer-like, syntax (obj->*f)
.)
Comments
Post a Comment