帮助增强绑定/功能

发布于 2024-08-04 17:42:29 字数 1011 浏览 8 评论 0原文

我有这个函数签名,我必须匹配

typedef int (*lua_CFunction) (lua_State *L);//target sig

这是到目前为止我所拥有的:

    //somewhere else... 
    ...
registerFunction<LuaEngine>("testFunc", &LuaEngine::testFunc, this);
    ...

    //0 arg callback
void funcCallback0(boost::function<void ()> func, lua_State *state)
{
    func();
}

template<typename SelfType>
void registerFunction(const std::string &funcName, boost::function<void (SelfType*)> func, SelfType *self)
{
            //funcToCall has to match lua_CFunction
    boost::function<void (lua_State *)> funcToCall = boost::bind(&LuaEngine::funcCallback0, this,
        boost::bind(func, self), _1);
    lua_register(_luaState, funcName.c_str(), funcToCall);
}

但是,在 lua_register(_luaState...),它仍然抱怨转换问题

错误 1 ​​错误 C2664: 'lua_pushcclosure':无法转换 参数2来自 'boost::function' 为 'lua_CFunction'

有人知道如何解决这个问题吗?

I have this function signature I have to match

typedef int (*lua_CFunction) (lua_State *L);//target sig

Here's what I have so far:

    //somewhere else... 
    ...
registerFunction<LuaEngine>("testFunc", &LuaEngine::testFunc, this);
    ...

    //0 arg callback
void funcCallback0(boost::function<void ()> func, lua_State *state)
{
    func();
}

template<typename SelfType>
void registerFunction(const std::string &funcName, boost::function<void (SelfType*)> func, SelfType *self)
{
            //funcToCall has to match lua_CFunction
    boost::function<void (lua_State *)> funcToCall = boost::bind(&LuaEngine::funcCallback0, this,
        boost::bind(func, self), _1);
    lua_register(_luaState, funcName.c_str(), funcToCall);
}

However, at lua_register(_luaState..., it's still complaining about conversion issues

Error 1 error C2664:
'lua_pushcclosure' : cannot convert
parameter 2 from
'boost::function' to
'lua_CFunction'

Anyone know how this can be solved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉黛浅 2024-08-11 17:42:29

这不能直接解决。 Lua API 想要您提供一个简单的函数指针 - 这只是一个代码指针,没有其他任何东西。同时,boost::function 是一个函数对象,它不可能转换为普通函数指针,因为 - 粗略地说 - 它不仅捕获代码,还捕获状态。在您的示例中,捕获的状态是 self 的值。因此它有一个代码指针和一些数据——目标 API 只需要代码指针。

This cannot be solved directly. Lua API wants a plain function pointers from you - that's just a code pointer, and nothing else. Meanwhile, boost::function is a function object, and there's no way it could possibly be convertible to a plain function pointer, because - roughly speaking - it captures not just the code, but also the state. In your example, the captured state is the value of self. So it has a code pointer for the code, and some data - and the target API expects just the code pointer.

蘑菇王子 2024-08-11 17:42:29

问题是编译器无法推断出模板参数,因为存在隐式转换。

您需要将函数指针存储到函数对象中。

function<int(lua_State *)> f = boost::bind(&LuaEngine::testFunc, this)
registerFunction<LuaEngine>("testFunc", f);

并且您的函数需要一个 void 返回类型,并且也需要更改为 int 。

The problem is that the compiler cant deduce the template parameter because there is an implicit conversion.

You need to store the function pointer into a function object.

function<int(lua_State *)> f = boost::bind(&LuaEngine::testFunc, this)
registerFunction<LuaEngine>("testFunc", f);

And your function expects a void return type and that is needed to change to int too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文