How to remove const specifier from member function pointer

I am using a library that contains the following code:

template <typename M> void _register_member(lua_State *state, const char *member_name, MT::*member) { std::function<M(T*)> lambda_get = [member](T *t) { //^ error here return t->*member; }; //... 

However, this code does not accept const function pointers. Passing these values ​​results in a Function cannot return function type 'void () const' error or regardless of the type of the const member function.

How to remove const specifier from passed member function or how to apply std::remove_const ?

+7
c ++ pointers c ++ 11 templates function-pointers
source share
2 answers

As Adam S noted in the comments, this error occurs when he tries to compile this simple code that uses the Selene library:

 #include <selene.h> class C { public: bool get() const; }; bool C::get() const {return true;} int main() { sel::State state; state["C"].SetClass<C>("get", &C::get); } 

The compiler cannot compile code in Class.h . It has two overloads of the member of the _register_member function of the Class class:

 template <typename T, typename A, typename... Members> class Class : public BaseClass { private: // ... template <typename M> void _register_member(lua_State *state, const char *member_name, MT::*member) { // ... } template <typename Ret, typename... Args> void _register_member(lua_State *state, const char *fun_name, Ret(T::*fun)(Args...)) { // ... } // ... }; 

The compiler cannot select the second overload when a pointer to an element of the const function is passed as the third argument. There must be another overload that can take a member of the const function. It should be declared as follows:

 template <typename Ret, typename... Args> void _register_member(lua_State *state, const char *fun_name, Ret(T::*fun)(Args...) const) ^^^^^ 

Without such an overload, the compiler selects the first overload, which is created to work with pointers to data elements (not members of a function) and cannot compile its code.

Thus, you cannot deal with const members when using the current version of the Selena library (in the way you do this, at least).

+1
source share

I should mention that for those who are currently viewing this, this is actually a mistake in my code (supervision really), and it was fixed shortly after the problem was identified.

+1
source share

All Articles