错误:从“Foo *”转换为“unsigned int”会丢失精度 [英] error: cast from 'Foo*' to 'unsigned int' loses precision
问题描述
我试图把一个指针指向一个int(或unsigned int),无论我尝试它不想工作。
我试过了 static_cast< intptr_t>(obj)
, reinterpret_cast< intptr_t& (obj)
,以及C风格转换的各种组合 intptr_t
', unsigned int
,我包括stdint.h。从我读过的,我尝试过的许多事情之一应该工作。什么给了?
我没有打扰包含代码,因为它正是我所描述的,但是自从你问以后,我试过所有这些加上其他组合: / p>
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;
// ...
}
。
您可以在 sizeof(Foo *)& sizeof(unsigned)
,或者您的编译器设置为关于非可移植代码的警告。请注意,大多数64位编译器,LP64和LLP64都属于此类别。
不需要指针适合 int
。这是 intptr_t
的整个点。
如果您使用的第三方库只提供 int
对于callbacls中的用户上下文,可以将索引传递到查找表中,因此指针本身存储在查找表中。这有额外的好处是类型安全,不打破别名假设。
编辑:适用于我。 ( Comeautryitout非常方便)
#include< stdint.h>
void myfunc(class Foo * obj)
{
uintptr_t temp = reinterpret_cast< uintptr_t>(obj);
}
Comeau C / C ++ 4.3.10.1 6 2008
11:28:09)for ONLINE_EVALUATION_BETA2
版权所有1988-2008 Comeau Computing。
保留所有权利。 MODE:strict
errors C ++ C ++ 0x_extensions
ComeauTest.c第5行:警告:
声明了变量temp从不
引用
uintptr_t temp = reinterpret_cast(obj); reinterpret_cast(obj);
在严格模式下,记住,Comeau在线编译器没有链接)。
编译时启用了C ++ 0x扩展。
在C89模式下,它也可以工作:
#include< stdint.h>
void myfunc(struct Foo * obj)
{
uintptr_t temp =(uintptr_t)obj;
}
Comeau C / C ++ 4.3.10.1 6 2008
11:28:09)for ONLINE_EVALUATION_BETA2
版权所有1988-2008 Comeau Computing。
保留所有权利。 MODE:strict
错误C90
ComeauTest.c,第3行:警告:
声明在
函数外部不可见void myfunc(struct Foo *
obj)
^
ComeauTest.c,第5行:warning:
variabletemp 被声明,但从不
引用
uintptr_t temp =(uintptr_t)obj;
^
在严格模式下,使用-tused,Compile
成功(但请记住,Comeau
在线编译器未链接) 。
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.
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).
这篇关于错误:从“Foo *”转换为“unsigned int”会丢失精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!