Search code examples
c++castingintptr

error: cast from 'Foo*' to 'unsigned int' loses precision


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.


Solution

  • 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 of intptr_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)

    #include <stdint.h>
    
    void myfunc(class Foo* obj)
    {
        uintptr_t temp = reinterpret_cast<uintptr_t>(obj);
    }
    

    Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2 Copyright 1988-2008 Comeau Computing. All rights reserved. MODE:strict errors C++ C++0x_extensions

    "ComeauTest.c", line 5: warning: variable "temp" was declared but never referenced uintptr_t temp = reinterpret_cast(obj);reinterpret_cast(obj);

    In strict mode, with -tused, Compile succeeded (but remember, the Comeau online compiler does not link). Compiled with C++0x extensions enabled.

    In C89 mode it also works:

    #include <stdint.h>
    
    void myfunc(struct Foo* obj)
    {
        uintptr_t temp = (uintptr_t)obj;
    }
    

    Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2 Copyright 1988-2008 Comeau Computing. All rights reserved. MODE:strict errors C90

    "ComeauTest.c", line 3: warning: declaration is not visible outside of function void myfunc(struct Foo* obj) ^

    "ComeauTest.c", line 5: warning: variable "temp" was declared but never referenced uintptr_t temp = (uintptr_t)obj; ^

    In strict mode, with -tused, Compile succeeded (but remember, the Comeau online compiler does not link).