什么是兼容“int”? 输入 32 位和 32 位 C++ 中的 64 位窗口?
C++ 中兼容的“int”数据类型是什么,可以在 32 位和 32 位上将自身大小调整为 4 个字节? 64 位 Windows 上是 8 个字节?
虽然 INT_PTR 工作得很好,但它降低了可读性,并且它的描述告诉我们将它用于指针运算。
谢谢
What is the compatible "int" datatype in C++ that can resize itself to 4 bytes on 32bit & 8 bytes on 64bit windows?
Although INT_PTR works fine but it reduces the readability as well as its description tells us to use it for pointer arithmetic.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您正在寻找标准的东西,那么您就不走运了。 该标准没有指定任何内置数据类型的大小。
请注意,
INT_PTR
并不意味着指针算术。 我的意思是该类型的大小与void *
相同,这正是您想要的。 但它并不适用于所有平台(我很确定它是 Windows 特定的)。If you're looking for something standard, you're out of luck. The standard does not specify the size of any of the built-in datatypes.
Note, that
INT_PTR
does not imply pointer arithmetic. I means that the type will have the same size asvoid *
, which is exactly what you want. It won't work on all platforms though (I'm pretty sure it's Windows specific).在 Visual Studio 下,您还提供 __int3264 ,它的作用与 INT_PTR 大致相同...
under Visual Studio you are also offered __int3264 which does much the same as INT_PTR ...
该标准没有提到具体的大小要求,只是每个整型类型必须提供至少与其之前的类型一样多的存储空间。 因此,
int
必须容纳与short
一样多的内容,依此类推。 您最好指定您需要它们的用途。如果您正在寻找不会根据操作环境改变大小的整数,请查看 Boost 整数库,或 C99/C++11 标头
。 其中包含uint32_t
/int32_t
和uintmax_t
/intmax_t
等类型。最重要的是,根据您的问题,它有:
uintptr_t
/intptr_t
。 这些保证具有正确的尺寸以将指针固定在您的平台上。The standard does not mention specific size requirements, only that each integral type must provide at least as much storage as the type before it. So
int
must hold as much as ashort
, and so on. You're better off specifying what you need them for.If you're looking for integers that do not change size based on the operating environment, take a look at the Boost Integer Library, or the C99/C++11 header
<cstdint>
. This contains types such asuint32_t
/int32_t
anduintmax_t
/intmax_t
.Most importantly, based off your question, it has:
uintptr_t
/intptr_t
. These are guaranteed to have the correct size to hold a pointer on your platform.这实际上取决于编译器。 我认为唯一(或多或少)可靠的方法是使用像 (void *) 这样的指针类型。
我认为最好的方法是在头文件中使用一些条件处理并设置自定义类型:(
此示例适用于 Visual C++)
It really depends on the compiler. I think the only (more or less) reliable way is by using a pointer type like (void *).
I think the best way is by using some conditional processing in your header file and set a custom type:
(this sample is for Visual C++)
这可能对您有帮助: http://lists.debian.org/debian -user/2006/04/msg00681.html。 不幸的是,你的问题似乎依赖于编译器。
This might help you: http://lists.debian.org/debian-user/2006/04/msg00681.html. Unfortunatly your question seems to be compiler dependant.