错误:从“Foo*”强制转换到“无符号整数”失去精确度
我正在尝试将指针强制转换为 int (或无符号 int),无论我尝试什么,它都不起作用。
我尝试过 static_cast
、reinterpret_cast
以及 C 风格转换的各种组合 intptr_t
's、unsigned int
's,并且我包括 stdint.h。根据我读到的内容,我尝试过的众多方法之一应该有效。什么给?
我没有费心包含代码,因为它正是我所描述的,但既然你问了,我已经尝试了所有这些加上其他组合:
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;
// ...
}
它们都给出了完全相同的错误。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所在的平台要么
sizeof (Foo*) > sizeof (unsigned)
,或者您的编译器设置为警告不可移植代码。请注意,大多数 64 位编译器(LP64 和 LLP64)都属于这一类。不要求指针必须位于
int
中。这就是intptr_t
的全部意义。如果您使用的第三方库在 callbacl 期间仅为用户上下文提供
int
,则可以将索引传递到查找表中,以便指针本身存储在查找表中。这样做的额外好处是类型安全并且不会破坏别名假设。编辑:对我有用。 (Comeau“tryitout” 非常方便)
在 C89 模式下它也可以工作:
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:
当然,最好掌握显式强制转换的类型转换。而且前面的回答已经说得很好了。
但我有一个绕过编译器的建议。有一个选项可以让编译器接受当前的精度损失:
Of course, it is better to master the type conversion by explicit cast. And the previous answers say it well.
But I have a suggestion to bypass the compiler. There is an option to let the compiler accept the current loss of precision: