C
christopher diggins
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.
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.