Take a look at this non-compiling snippet:
struct Object { template <RETURN (OBJECT::*MEMFN)(PARAMETERS...), typename RETURN, typename OBJECT, typename ...PARAMETERS> void call() { } }; struct Foo { void fn(); }; int main() { Object o; o.call<&Foo::fn>(); }
Basically, I want to get a function ( Object::call ) that specializes in any member function pointers, with convenient syntax for calling.
The closest solution I found is that is very ugly:
struct Object { }; template <typename MEMFNTYPE, MEMFNTYPE MEMFN> struct Caller; template <typename RETURN, typename OBJECT, typename ...PARAMETERS, RETURN (OBJECT::*MEMFN)(PARAMETERS...)> struct Caller<RETURN (OBJECT::*)(PARAMETERS...), MEMFN> { Object *object; Caller(Object &o) : object(&o) { } void call() {
Is there a better solution to this problem?
(The reason that I am trying to do this is that I would like to create wrapper functions ( call ) for other member functions)
Kerrek SB suggested using auto , I tried this:
struct Object { template <auto MEMFN> void call(); template <auto MEMFN, typename RETURN, typename OBJECT, typename ...PARAMETERS> void call<RETURN (OBJECT::*MEMFN)(PARAMETERS...)>() { } }; struct Foo { void fn(); }; int main() { Object o; o.call<&Foo::fn>(); }
However, this does not compile (I need to add MEMFN differently to the list of template parameters (instead of auto MEMFN ?):
t2.cpp:6:7: error: parse error in template argument list void call<RETURN (OBJECT::*MEMFN)(PARAMETERS...)>() { ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ t2.cpp:6:52: error: non-class, non-variable partial specialization 'call<<expression error> >' is not allowed void call<RETURN (OBJECT::*MEMFN)(PARAMETERS...)>() {