C++ 在 void * 和指向成员函数的指针之间进行转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1307278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Casting between void * and a pointer to member function
提问by quark
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:
我目前正在使用 GCC 4.4,而且我在转换void*
成员函数和指向成员函数的指针之间感到非常头疼。我正在尝试编写一个易于使用的库,用于将 C++ 对象绑定到 Lua 解释器,如下所示:
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?
对于那些不熟悉 Lua 的人,lua_touserdata(L, lua_upvalueindex(1))
获取与闭包关联的第一个值(在本例中,它是指向成员函数的指针)并将其作为void*
. GCC 抱怨void*
->void (T::*)(int, int)
是无效的转换。关于如何解决这个问题的任何想法?
回答by quark
You cannot cast a pointer-to-member to void *
or to any other "regular" pointer type. Pointers-to-members are not addresses the way regular pointers are. What you most likely will need to do is wrap your member function in a regular function. The C++ FAQ Lite explains thisin some detail. The main issue is that the data needed to implement a pointer-to-member is not just an address, and in fact varies tremendouslybased on the compiler implementation.
您不能将指向成员的指针转换为void *
或转换为任何其他“常规”指针类型。指向成员的指针不是常规指针那样的地址。您最有可能需要做的是将您的成员函数包装在一个常规函数中。C++ FAQ Lite详细解释了这一点。主要问题是实现成员指针所需的数据不仅仅是一个地址,实际上根据编译器的实现而有很大的不同。
I presume you have control over what the user data lua_touserdata
is returning. It can't be a pointer-to-member since there isn't a legal way to get this information back out. But you do have some other choices:
我认为您可以控制用户数据lua_touserdata
返回的内容。它不能是指向成员的指针,因为没有合法的方式来获取此信息。但你还有其他一些选择:
The simplest choice is probably to wrap your member function in a free function and return that. That free function should take the object as its first argument. See the code sample below.
Use a technique similar to that of Boost.Bind's mem_funto return a function object, which you can template on appropriately. I don't see that this is easier, but it would let you associate the more state with the function return if you needed to.
最简单的选择可能是将您的成员函数包装在一个自由函数中并返回它。该自由函数应将对象作为其第一个参数。请参阅下面的代码示例。
使用类似于Boost.Bind 的 mem_fun的技术返回一个函数对象,您可以适当地对其进行模板化。我不认为这更容易,但是如果需要,它可以让您将更多状态与函数返回相关联。
Here's a rewrite of your function using the first way:
这是使用第一种方式重写您的函数:
template <class T>
int call_int_function(lua_State *L)
{
void (*method)(T*, int, int) = reinterpret_cast<void (*)(T*, int, int)>(lua_touserdata(L, lua_upvalueindex(1)));
T *obj = reinterpret_cast<T *>(lua_touserdata(L, 1));
method(obj, lua_tointeger(L, 2), lua_tointeger(L, 3));
return 0;
}
回答by paniq
It is possible to convert pointer to member functions and attributes using unions:
可以使用联合将指针转换为成员函数和属性:
// helper union to cast pointer to member
template<typename classT, typename memberT>
union u_ptm_cast {
memberT classT::*pmember;
void *pvoid;
};
To convert, put the source value into one member, and pull the target value out of the other.
要进行转换,请将源值放入一个成员中,并将目标值从另一个中拉出。
While this method is practical, I have no idea if it's going to work in every case.
虽然这种方法很实用,但我不知道它是否适用于所有情况。
回答by Karlo Mili?evi?
Here, just change the parameters of the function void_cast for it to fit your needs:
在这里,只需更改函数 void_cast 的参数即可满足您的需求:
template<typename T, typename R>
void* void_cast(R(T::*f)())
{
union
{
R(T::*pf)();
void* p;
};
pf = f;
return p;
}
example use:
示例使用:
auto pvoid = void_cast(&Foo::foo);
回答by Kaz
Unlike the address of a nonstaticmember function, which is a pointer-to-member type with a complicated representation, the address of a staticmember function is usually a just a machine address, compatible with a conversion to void *
.
与非静态成员函数的地址不同,它是具有复杂表示的指向成员类型的指针,静态成员函数的地址通常只是一个机器地址,兼容转换为void *
。
If you need to bind a C++ non-static member function to a C or C-like callback mechanism based on void *
, what you can try to do is write a static wrapper instead.
如果您需要将 C++ 非静态成员函数绑定到基于 的 C 或类 C 回调机制void *
,您可以尝试做的是编写一个静态包装器。
The wrapper can take a pointer to an instance as an argument, and pass control to the nonstatic member function:
包装器可以将指向实例的指针作为参数,并将控制权传递给非静态成员函数:
void myclass::static_fun(myclass *instance, int arg)
{
instance->nonstatic_fun(arg);
}
回答by fbrereto
As a workaround given the restrictions of casting a pointer-to-member-function to void*
you could wrap the function pointer in a small heap-allocated struct and put a pointer to that struct in your Lua user data:
考虑到将指针指向成员函数的限制,作为一种解决方法,void*
您可以将函数指针包装在一个小的堆分配结构中,并在您的 Lua 用户数据中放置一个指向该结构的指针:
template <typename T>
struct LuaUserData {
typename void (T::*MemberProc)(int, int);
explicit LuaUserData(MemberProc proc) :
mProc(proc)
{ }
MemberProc mProc;
};
LuaObject<Foo> lobj = registerObject(L, "foo", fooObject);
LuaUserData<Foo>* lobj_data = new LuaUserData<Foo>(&Foo::bar);
lobj.addField(L, "bar", lobj_data);
// ...
template <class T>
int call_int_function(lua_State *L)
{
typedef LuaUserData<T> LuaUserDataType;
typedef typename LuaUserDataType::MemberProc ProcType;
// this next line is problematic
LuaUserDataType* data =
reinterpret_cast<LuaUserDataType*>(lua_touserdata(L, lua_upvalueindex(1)));
T *obj = reinterpret_cast<T *>(lua_touserdata(L, 1));
(obj->*(data.mMemberProc))(lua_tointeger(L, 2), lua_tointeger(L, 3));
return 0;
}
I'm not savvy with Lua so I have likely overlooked something in the above example. Keep in mind, too, if you go this route you'll have to manage the LuaUserData's allocation.
我对 Lua 不精通,所以我可能忽略了上面示例中的某些内容。还要记住,如果你走这条路,你将不得不管理 LuaUserData 的分配。