#include <utility>
class Base {
public:
virtual ~Base() {}
virtual void base() {}
};
class Derived : public Base {
public:
virtual void derived() {}
};
template<typename... Params>
using MemberFuncPtr = void(Derived::*)(Params...);
template<typename... Params, typename... Args>
void wrapper(MemberFuncPtr<Params...> ptr, Args&&... args)
{
Derived* d = new Derived();
(d->*ptr)(std::forward<Args>(args)...);
delete d;
}
int main()
{
wrapper(&Derived::derived);
wrapper(&Derived::base);
return 0;
}
Trying to run this code (GCC 7.0) gives me the following error:
prog.cc: In function 'int main()':
prog.cc:33:27: error: no matching function for call to 'wrapper(void (Base::*)())'
wrapper(&Derived::base);
^
prog.cc:18:6: note: candidate: template<class ... Params, class ... Args> void wrapper(MemberFuncPtr<Params ...>, Args&& ...)
void wrapper(MemberFuncPtr<Params...> ptr, Args&&... args)
^~~~~~~
prog.cc:18:6: note: template argument deduction/substitution failed:
prog.cc:33:27: note: mismatched types 'Derived' and 'Base'
wrapper(&Derived::base);
^
I don't really understand why method from the base class is an issue? It's also a method for derived class. I've done simple test where I assigned Derived::base to Derived::*ptr type and that worked.