我正在寻找一种数据类型(将用作索引),它应该是32位操作系统中的16位和64位操作系统中的32位。我使用VS2010,我知道可以使用HALF_PTR,但是当用于索引时听起来不太好我仍然可以将它定义为更有意义的名称,但在此之前我可以使用任何已定义的数据类型这个目的?
答案 0 :(得分:5)
您可以使用特征制作适合您系统的类型别名:
#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;
现在使用HalfPtr
作为您的类型。
(您可以通过使用std::conditional
几次来快速完成上述操作。许多方法可以为这只猫提供皮肤。)