c++ - Passing pointer to any member function as class template argument -
template<typename t, typename m, m method> class proxyobject { public: template<typename... args> void invoke (t& object, _in_ args&&... a) { (void)(object.*method)(std::forward<args>(a)...); } }; class object { public: int mymethod (int val) { wcout << l"hello!" << endl; return val; } }; int wmain () { object myobj; proxyobject<object, decltype(&object::mymethod), &object::mymethod> obj; obj.invoke(myobj, 10); return 0; }
the decltype(&object::mymethod)
seems redundant in definition of obj
. there way make proxyobject
automatically infer type of pointer-to-member-function being passed, can define obj
follows:
proxyobject<object, &object::mymethod> obj;
i think it's impossible class template, because have specify member function type explicitly.
template function lot argument deduction:
template<typename t, typename m, typename... args> void invoke (t& object, m method, args&&... a) { (void)(object.*method)(std::forward<args>(a)...); }
then
invoke(myobj, &object::mymethod, 10);
Comments
Post a Comment