简体   繁体   English

如何从成员函数指针中删除const限定符

[英]How to remove const qualifier from a member function pointer

I'm using a library which contains the following code: 我正在使用包含以下代码的库:

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

However this code does not accept const member function pointers. 但是,此代码不接受const成员函数指针。 Passing those yields the error Function cannot return function type 'void () const' or whatever the type of the const member function is. 传递那些产生错误Function cannot return function type 'void () const'或const成员函数的任何类型。

How do I remove the const qualifier from the passed member function or how do I apply std::remove_const ? 如何从传递的成员函数中删除const限定符或如何应用std::remove_const

As Adam S noted in comments this error occurs when he tries to compile this simple code which uses the library Selene : 正如Adam S在评论中指出的那样,当他试图编译使用库Selene的 简单代码时会发生这种错误:

#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 fails to compile the code in Class.h header. 编译器无法编译Class.h头中的代码。 There are two overloads of function member _register_member of the class Class in it: 其中类Class的函数成员_register_member有两个重载:

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,
                          M T::*member) {
        // ...
    }

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

    // ...

};

The compiler can't choose the second overload when a pointer to a const function member is passed as a third argument. 当指向const函数成员的指针作为第三个参数传递时,编译器不能选择第二个重载。 There should be another overload which could accept a const function member. 应该有另一个可以接受const函数成员的重载。 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 overload the compiler chooses the first overload which is created to work with pointers to data members (not function members) and fails to compile its code. 如果没有这样的重载,编译器会选择创建的第一个重载来处理指向数据成员(不是函数成员)的指针,并且无法编译其代码。

So you can't deal with const function members when using current version of Selena library (in such way as you do it at least). 因此,在使用当前版本的Selena库时,您无法处理const函数成员(至少这样做)。

我应该提一下,对于那些现在看到这个,这实际上是我的代码中的一个错误(真的是疏忽)并且在问题被识别后不久就修复了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM