I have the following code base:
template <typename Type> class SomeClass { public: template <typename ReturnType, typename... Params> void register_function(const std::pair<std::string, ReturnType (Type::*)(Params...)> fct) { auto f = [fct](Params... params) -> ReturnType { return (Type().*fct.second)(std::ref(params)...); }
This works when I pass a pointer to a member function (not a constant). However, if I want to pass a pointer to a constant member function, this will lead to a compilation error, and I have to duplicate the above function in order to get this code:
template <typename Type> class SomeClass { public: template <typename ReturnType, typename... Params> void register_function(const std::pair<std::string, ReturnType (Type::*)(Params...)> fct) { auto f = [fct](Params... params) -> ReturnType { return (Type().*fct.second)(std::ref(params)...); }
Now I can pass both const-member functions and non-const-member functions. But now the code is duplicated, and maintainability is reduced.
Is there a way to combine these two functions into a function that accepts both const-member-functions and non-const-member-functions?
Important note: I really have to use the pointer function as a parameter (no std :: function).
Edit: I added a bit more code. Inside the functions, I build a closure corresponding to the signature of the member function (the same types of returned parameters and parameters). This closure will be saved and used later for reflection ( more details here )
source share