I want to implement a generic function that will take reference to object and pointer to its member function and invoke it. However I'm not able to do so when my class has both const and non-const methods as I need to provide two overloads:
template<typename Ret, typename Class, typename ...Us>
Ret callMethod(Class &object, Ret (Class::*method)(Us...))
{
return (object.*method)(Us{}...);
}
template<typename Ret, typename Class, typename ...Us>
Ret callMethod(Class &object, Ret (Class::*method)(Us...) const)
{
return (object.*method)(Us{}...);
}
Is there some way to write only 1 template function that will accept both const and non-const method pointers so I don't have to write my code twice? I'm using C++14.
For a broader picture, what I want to ultimately achieve is pass a 3rd parameter, a data buffer from which method arguments will be extracted - hence the template function to handle it as generically as possible.