简体   繁体   English

函数指针的C ++ lua桥问题

[英]C++ lua bridge problems with function pointers

I am trying to create a simple c++ wraper over *lua_CFunction* and it is defined like : 我试图在* lua_CFunction *上创建一个简单的c ++包装器,其定义如下:

// header
typedef int (*lua_CFunction) (lua_State* lua);
...
lua_CFunction wrap (std::function <int (Game* game)> function);
// implementation
lua_CFunction ScriptingInterface::wrap (std::function <int (Game* game)> function) 
{
    return [this, function] (lua_State* unused) -> int {
        int n_args = function (this->game);
        return n_args;
    };
}
void ScriptingInterface::registerFunction (std::string name, std::function <int (Game* game)> function) 
{
    lua_register (lua, name.c_str (), wrap (function));
}

The idea is to create public functions like this : 这个想法是创建这样的公共功能:

int setTitle (Game* interface) 
{
    const char* title = lua_tostring (interface->getScripts ()->getLuaState (), 1);

    SDL_WM_SetCaption (title, NULL);

    return 0;
}

And share them with lua like : 并与lua共享它们,例如:

scripts->registerFunction ("setTitle", setTitle);

scripts is an instance of ScriptingInterface 脚本ScriptingInterface的一个实例

The problem occurs when trying to compile the game. 尝试编译游戏时出现问题。

./scripting/scripting_interface.cc: In member function ‘int (* ScriptingInterface::wrap(std::function<int(Game*)>))(lua_State*)’:
./scripting/scripting_interface.cc:40:2: error: cannot convert ‘ScriptingInterface::wrap(std::function<int(Game*)>)::<lambda(lua_State*)>’ to ‘int (*)(lua_State*)’ in return
./scripting/scripting_interface.cc:41:1: warning: control reaches end of non-void function [-Wreturn-type]

Could anyone tell me what I am doing wrong in here, because AFAIK the code should compile without any problem? 谁能告诉我我在这里做错了什么,因为AFAIK代码可以毫无问题地进行编译?

The problem is here: 问题在这里:

lua_CFunction ScriptingInterface::wrap(std::function<int(Game*)> function) 
{
    return [this, function] (lua_State* unused) -> int {
        int n_args = function (this->game);
        return n_args;
    };
}

You are trying to return a lambda where a function pointer is expected, but a capturing lambda cannot be converted to a function pointer - and your lambda is capturing both this and function . 您试图返回一个需要函数指针的lambda,但是捕获的 lambda无法转换为函数指针-并且您的lambda既捕获了this又捕获了function Per Paragraph 5.1.2/6 of the C++11 Standard: 根据C ++ 11标准的5.1.2 / 6段:

The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type's function call operator. 没有lambda捕获的lambda表达式的闭包类型具有指向该函数的指针的公共非虚拟非显式const转换函数,该函数具有与闭包类型的函数调用运算符相同的参数和返回类型。 The value returned by this conversion function shall be the address of a function that, when invoked, has the same effect as invoking the closure type's function call operator. 该转换函数返回的值应是一个函数的地址,该函数在被调用时与调用闭包类型的函数调用操作符具有相同的作用。

Unfortunately, unless you can return an std::function<int(lua_State*)> , you will have to change your design. 不幸的是,除非您可以返回std::function<int(lua_State*)> ,否则您将不得不更改设计。

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

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