c++11 - C++ Functor template for class member functions -
there small functor class wrote should able call class member functions hiding static wrapper function , void pointer object. example below not compile due error when setting wrapper function. want class member pointer template argument. know wrong there?
i think there problem in static function when calling member function. not know how template syntax. minimal example compiles c++11 enabled gcc.
#include <iostream> template<class treturn, class... tparameter> struct functor { treturn (*ptr)(void*, tparameter...); void *object; template<class tobject, class tmemberfunction> static treturn membercaller(void *obj, tparameter... params) { tobject *c = static_cast<tobject*>(obj); return (c->*(tobject::tmemberfunction))(params...); } treturn operator()(tparameter... params) { return ptr(object, params...); } }; class test { public: void func(int a) { std::cout << << std::endl; } }; int main(int argc, const char **argv) { functor<void, int> f; test t; f.object = &t; f.ptr = &functor<void, int>::membercaller<test, test::func>; f(100); }
will work you?
#include <iostream> template<class tobject, class t, class... tparameter> struct functor { using tmemberfunction = t (tobject::*)(tparameter...); tmemberfunction ptr; tobject *object; t operator()(tparameter... params) { return (object->*ptr)(params...); } }; class test { public: void func(int a) { std::cout << << std::endl; } }; template<typename t> class td; int main() { functor<test, void , int> f; test t; f.object = &t; f.ptr = &test::func; f(100); }
Comments
Post a Comment