1

是否有为“无符号整数”定义的缩短标签 typedef 可以在大多数/所有平台上使用?我在 Windows 中看到了很多 uint 或 UINT,但我知道这在其他平台上并没有一致地实现。我很乐意为每个实例输入 'unsigned int',但如果有标准的 typedef(如 'uint',虽然没有始终如一地实现),我会使用它。我怀疑在做了一些研究后情况并非如此,但人们可能对例如有一些了解。C++11 等,或跨 *nix/Windows 实现相同标签的标准头文件。我对严格指定 unsigned int 的位大小不太感兴趣:它仅用于低迭代循环。

4

3 回答 3

2

的缩写unsigned intunsigned。同理,long int可简写为longunsigned long int又可简写为unsigned long

这条规则有一个例外:signed charunsigned char不能缩写为char.

于 2013-09-15T23:14:21.130 回答
1

Basically, type sizes are defined by the compiler that you're using and the platform that your target program (the executable) will run on.

For example, on an x86 machine, almost all compilers will take int (and hence unsigned int) to mean a standard 32-bit integer. However, on a hypothetical 128-bit CPU, int will probably refer to a 64-bit or 128-bit integer.

However, if you are like me and insist on knowing what the size of your integer is at all times, I suggest you check out the cstdint header, which was introduced in C++11.

This allows you to specify integers of particular sizes (e.g. uint32_t) independently of the compiler and platform. Beware though, if a particular platform doesn't support that size, your program will either not compile.

Check out this site for more details: http://www.cplusplus.com/reference/cstdint/ Use #include <stdint.h> for C and #include <cstdint> for C++.

于 2013-09-15T12:24:53.097 回答
1

语言标准和标准库没有为类型定义任何“琐碎”的别名,这些别名除了提供替代名称之外没有任何用途。

标准库确实定义了几个具有平台相关含义的类型别名。例如,整数类型std::uintptr_t是一个大到足以表示任何对象指针值的整数类型;std::max_align_t是一个类型,它的alignof-value 是平台提供的最大的。其他类型别名是有条件定义的std::uint32_t等,如果它们存在,它们是具有二进制补码表示的固定宽度的整数类型。当然,这些只是基本类型之一的 typedef,但含义不同,而且这些都不是微不足道的,因此拥有这些 typedef 是有充分理由的。

于 2013-09-15T15:18:44.060 回答