I'm trying to cast a pointer to an int (or unsigned int) and no matter what I try it doesn't want to work.
I've tried static_cast<intptr_t>(obj)
, reinterpret_cast<intptr_t>(obj)
, and various combinations of C style casts, intptr_t
's, unsigned int
's, and I'm including stdint.h. From what I've read, one of the many things I've tried should work. What gives?
I didn't bother including the code because it's exactly what I described, but since you asked, I've tried all of these plus other combinations:
void myfunc(Foo* obj)
{
// ...
uintptr_t temp = reinterpret_cast<uintptr_t>(obj);
uintptr_t temp = static_cast<uintptr_t>(obj);
uintptr_t temp = (uintptr_t)obj;
intptr_t temp = reinterpret_cast<intptr_t>(obj);
intptr_t temp = static_cast<intptr_t>(obj);
intptr_t temp = (intptr_t)obj;
unsigned int temp = reinterpret_cast<unsigned int>(obj);
unsigned int temp = static_cast<unsigned int>(obj);
unsigned int temp = (unsigned int)obj;
// ...
}
They all give the exact same error.
Of course, it is better to master the type conversion by explicit cast. And the previous answers says it well.
But I have a suggestion to bypass the compiler. There is an option to let the compiler accept the actual loss of precision:
You're either on a platform where
sizeof (Foo*) > sizeof (unsigned)
, or your compiler is set to warn about non-portable code. Note that most 64-bit compilers, both LP64 and LLP64, fall into this category.There's no requirement that a pointer fit in an
int
. That's the whole point ofintptr_t
.If you're using a third-party library that provides only a
int
for user-context during callbacls, you could pass an index into a lookup table, so the pointer itself is stored in the lookup table. This has the additional benefit of being type-safe and not breaking aliasing assumptions.EDIT: Works for me. (Comeau "tryitout" is very handy)
In C89 mode it also works: