gpt4 book ai didi

c++ - 帮助提升绑定(bind)/功能

转载 作者:行者123 更新时间:2023-11-30 01:32:35 24 4
gpt4 key购买 nike

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

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...时,还是报错转换问题

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

有人知道怎么解决吗?

最佳答案

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

关于c++ - 帮助提升绑定(bind)/功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1413744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com