什么数据类型是32位操作系统中的16位和64位操作系统中的32位?

时间:2022-03-10 16:23:51

I'm looking for a datatype (which will be used as an Index), it should be 16 bits in 32-bit os and 32-bits in 64-bit OS. I use VS2010, and I'm aware that HALF_PTR can be used but it doesn't sound good when used for index and I can still typedef it to a more meaningful name, but before that is there any defined datatype that I can use for this purpose?

我正在寻找一种数据类型(将用作索引),它应该是32位操作系统中的16位和64位操作系统中的32位。我使用VS2010,我知道可以使用HALF_PTR,但是当用于索引时听起来不太好我仍然可以将它定义为更有意义的名称,但在此之前我可以使用任何已定义的数据类型这个目的?

1 个解决方案

#1


5  

You can use traits to make a type alias that suits your system:

您可以使用traits创建适合您系统的类型别名:

#include <cstdint>

template <unsigned int> struct HalfPtrImpl;
template <> struct HalfPtrImpl<4> { typedef uint16_t type; };
template <> struct HalfPtrImpl<8> { typedef uint32_t type; };

typedef HalfPtrImpl<sizeof(uintptr_t)>::type HalfPtr;

Now use HalfPtr as your type.

现在使用HalfPtr作为您的类型。

(You can shortcut the above by using std::conditional a couple of times. Many ways to skin this cat.)

(您可以通过使用std :: conditional几次来快速执行上述操作。许多方法可以为这只猫提供皮肤。)

#1


5  

You can use traits to make a type alias that suits your system:

您可以使用traits创建适合您系统的类型别名:

#include <cstdint>

template <unsigned int> struct HalfPtrImpl;
template <> struct HalfPtrImpl<4> { typedef uint16_t type; };
template <> struct HalfPtrImpl<8> { typedef uint32_t type; };

typedef HalfPtrImpl<sizeof(uintptr_t)>::type HalfPtr;

Now use HalfPtr as your type.

现在使用HalfPtr作为您的类型。

(You can shortcut the above by using std::conditional a couple of times. Many ways to skin this cat.)

(您可以通过使用std :: conditional几次来快速执行上述操作。许多方法可以为这只猫提供皮肤。)