将anythign转换为void *并返回的最佳方式 [英] Best way to cast anythign to void* and back
问题描述
您好,
我想将类型为T的值(其中sizeof(T)< = sizeof(void *))转换为
void *又回来了。我目前正在按如下方式进行:
void ** ppvoid = reinterpret_cast< void **>(const_cast< T *>(& x));
void * x = * ppvoid;
这适用于Visual C ++和GCC 3.4,但我怀疑它不是正确的
做事的方式。
我认为工会方法:
模板< typename T>
union cast_union {
void * pvoid;
T x;
}
但这不是''因为T可能有非平凡的构造函数。
任何帮助都会非常感激。
-
Christopher Diggins
http://www.cdiggins.com >
推荐答案
christopher diggins写道:
christopher diggins wrote:
我想转换类型为T的值,其中sizeof(T)< = sizeof(void *)进入
void *然后再回来。
好的。我想这是为了传递某种第三方功能来将
传递回你的回调函数......
我目前正在做如下:
void ** ppvoid = reinterpret_cast< void **>(const_cast< T *>(& x));
void * x = * ppvoid;
我强烈建议让操作员无效*和构造函数
构造自''void *''。
这适用于Visual C ++和GCC 3.4,但我怀疑它不是正确的做事方式。
我认为联合方法:
> template< typename T>
union cast_union {
void * pvoid;
T x;
}
但这不起作用,因为T可能有非平凡的构造函数。
I want to convert a value of type T where sizeof(T) <= sizeof(void*) into a
void* and back again.
OK. I suppose it''s for passing to some kind of third-party function to
be passed back to your callback function...
I am currently doing it as follows:
void** ppvoid = reinterpret_cast<void**>(const_cast<T*>(&x));
void* x = *ppvoid;
I would strongly recommend just having an operator void* and a constructor
that constructs from ''void*''.
This works on Visual C++ and GCC 3.4 but I suspect it is not the "correct"
way to do things.
I considered the union approach:
template<typename T>
union cast_union {
void* pvoid;
T x;
}
But this doesn''t work because T might have non-trivial constructors.
V
V
" christopher狄金斯" < CD ****** @ videotron.ca>在消息中写道
新闻:我们******************** @ weber.videotron.net ...
"christopher diggins" <cd******@videotron.ca> wrote in message
news:We********************@weber.videotron.net...
您好,
我想将类型为T的值(其中sizeof(T)< = sizeof(void *)转换为
一个void *并再次返回)。我目前正在按如下方式进行:
void ** ppvoid = reinterpret_cast< void **>(const_cast< T *>(& x));
void * x = * ppvoid;
这适用于Visual C ++和GCC 3.4但我怀疑它不是正确的做事方式。
我认为工会方法:
模板< typename T>
工会cast_union {
void * pvoid;
T x;
}
>但是这不起作用,因为T可能有非平凡的构造函数。
任何帮助都会非常感激。
Hello,
I want to convert a value of type T where sizeof(T) <= sizeof(void*) into
a void* and back again. I am currently doing it as follows:
void** ppvoid = reinterpret_cast<void**>(const_cast<T*>(&x));
void* x = *ppvoid;
This works on Visual C++ and GCC 3.4 but I suspect it is not the "correct"
way to do things.
I considered the union approach:
template<typename T>
union cast_union {
void* pvoid;
T x;
}
But this doesn''t work because T might have non-trivial constructors.
Any help would be most appreciated.
嗯,如果类型''T''不是指针类型,那么你就超出了标准C ++的
领域。你可以转换为/从没有UB或IDB的唯一类型是
''void *''是其他指针类型。检查每个编译器的
文档,看看它是否/如何实现这样的转换。
-Mike
Well, if type ''T'' is not a pointer type, you''re outside the
realm of standard C++. The only types you can convert to/from
''void*'' without UB or IDB are other pointer types. Check the
documentation for each compiler you use to see if/how it
implements such conversions.
-Mike
" Victor Bazarov" <五******** @ comAcast.net>在消息中写道
新闻:Lu ****************** @ newsread1.mlpsca01.us.to .verio.net ...
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Lu******************@newsread1.mlpsca01.us.to .verio.net...
christopher diggins写道:
christopher diggins wrote:
我想将类型为T的值转换为sizeof(T)< = sizeof(void *)为
一个void *并再次返回。
I want to convert a value of type T where sizeof(T) <= sizeof(void*) into
a void* and back again.
好的。我想这是为了传递某种第三方功能来传递给你的回调函数......
OK. I suppose it''s for passing to some kind of third-party function to
be passed back to your callback function...
No. I我正在做一些更险恶的事情;-)
No. I am doing something much more sinister ;-)
我目前正在按如下方式进行:
void ** ppvoid = reinterpret_cast< void **>(const_cast< T *>(& x));
void * x = * ppvoid;
I am currently doing it as follows:
void** ppvoid = reinterpret_cast<void**>(const_cast<T*>(&x));
void* x = *ppvoid;
我强烈建议你拥有一个运算符void *和一个构造函数
构造自''void *''。
I would strongly recommend just having an operator void* and a constructor
that constructs from ''void*''.
不幸的是我无法修改。
-
Christopher Diggins
http://www.cdiggins.com
这篇关于将anythign转换为void *并返回的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!