【发布时间】:2015-12-02 16:22:39
【问题描述】:
我正在使用可变参数调用模式shown here。它适用于普通的 C 函数。但是我想将它与一个成员函数和一个显式地将实例指针作为第一个参数的元组一起使用。
我尝试过这样的事情:
template<typename Ret, typename C, typename...Args>
int methodWrapper(const string& name, Ret (C::*func)(Args...), lua_State* L)
{
return wrap<Ret,C*,Args...>::wrapFunc(name, func, L);
}
但是转换指针类型失败:
cannot initialize a parameter of type 'void (*)(Object *, float, float)' with an lvalue of type 'void (Object::*)(float, float)'
在这种情况下,wrap 函数是模板化的,因此指针的类型必须完全匹配。
template<typename Ret, typename...Args>
struct wrap{
static int wrapFunc(const string& name, Ret (*func)(Args...), lua_State* L)
{
...
}
};
如何将成员函数指针显式转换为以 this 指针为第一个参数的普通函数指针?
编辑:对于那些将我链接到this question 的人来说,情况不一样。我不必将纯 C 函数指针传递给具有特定接口的现有 API。而且我不想将成员函数绑定到特定对象。这个问题也不涉及可变参数调用或元组。
【问题讨论】:
-
或者stackoverflow.com/questions/19808054/…,如果你愿意的话
-
您应该知道
lua_CFunction的类型为extern "C" int (*) (lua_State *L)。你可以传递int (*)(lua_State *L)几乎是每个编译器中的一个错误,并且不符合标准。 -
创建一个 lambda 并将其传入怎么样?
wrapFunc接收到的函数有什么作用?
标签: c++ templates pointers function-pointers variadic-templates