我想做的基本上是能够调用一个函数并让它向 lua 注册一个 c++ 函数。基本上类似于 registerFunction(function)。现在,我知道有些库可以为您执行此操作,但我想了解有人会如何编写这样的库。
我目前的方法是使用模板为传递给它的函数生成相关的胶水函数。
我的代码现在是这样的:
template<typename F>
struct registerStruct
{
template<typename T>
struct inner
{
static int func(lua_State*);
};
};
template<typename F>
void registerFunction(const char *name,F function)
{
lua_register(this->L,name,®isterStruct<decltype(function)>::inner<function>::func);
}
template<typename F>
struct registerStruct<F(void)> //I would write more classes for different numbers of arguments
{
template<F(*T)(void)>
struct inner
{
static int func(lua_State *L)
{
returnLua(L,T()); //push the return value onto lua's stack
return 1;
}
};
};
然后我尝试这样使用它:
int test(void)
{
std::cout<<"hello from c++"<<std::endl;
return 0;
}
registerFunction("test",test);
用 gcc 编译产生错误::func has not been declared。
最佳答案
registerStruct<decltype(function)>::inner<function>::func
一方面,decltype(function)
可以替换为 F
.
此外,类模板 inner
需要一个类型作为它的模板参数,并且 function
是一个表达式,而不是一个类型。那可能应该是 ::inner<F>
.
(或者可能 inner
根本不需要是模板。嵌套类 outer<T>::inner
仍然可以使用附加到其封闭类的模板参数 T
。)
C++ 语法需要一些奇怪的东西来帮助解析器弄清楚模板的含义。通常 C++ 的含义取决于标识符是用于变量(对象或引用)、类型还是模板。但是经过 ::
或 .
或 ->
在模板中,如果运算符左侧的类型取决于模板参数,则不可能在初始解析期间找出下一个名称是哪个。如果您不帮忙,语言将假定该名称是一个变量。
所以C++认为registerStruct<decltype(function)>::inner
是一个变量。然后我们有小于运算符 <
,后跟有效表达式 function
,然后是大于运算符 >
, 然后 ::func
.等等,没有什么叫做 ::func
!
指定inner
真的是一个模板,你需要
®isterStruct<F>::template inner<F>::func
关于c++ -::func has not been declared 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17056111/