Hello Stackoverflow,
I'm currently using GCC 4.4, and I'm having quite the headache casting between void * and a pointer to member function. I'm trying to write an easy-to-use library for binding C++ objects to a Lua interpreter, like so:
LuaObject<Foo> lobj = registerObject(L, "foo", fooObject);
lobj.addField(L, "bar", &Foo::bar);
I've got most of it done, except for the following function (which is specific to a certain function signature until I have a chance to generalize it):
template <class T>
int call_int_function(lua_State *L)
{
// this next line is problematic
void (T::*method)(int, int) = reinterpret_cast<void (T::*)(int, int)>(lua_touserdata(L, lua_upvalueindex(1)));
T *obj = reinterpret_cast<T *>(lua_touserdata(L, 1));
(obj->*method)(lua_tointeger(L, 2), lua_tointeger(L, 3));
return 0;
}
For those of you unfamiliar with Lua, lua_touserdata(L, lua_upvalueindex(1))
gets the first value associated with a closure (in this case, it's the pointer to member function) and returns it as a void *
. GCC complains that void *
-> void (T::*)(int, int) is an invalid cast. Any ideas on how to get around this?
Thanks, Rob