简体   繁体   English

<stdint.h>还是标准类型?

[英]<stdint.h> or standard types?

Which types should I use when programming C++ on Linux? 在Linux上进行C ++编程时应该使用哪些类型? Is it good idea to use types from stdint.h , such as int16_t and uint8_t ? 使用stdint.h类型(例如int16_tuint8_t是个好主意吗?

On one hand, surely stdint.h won't be available for programming on Windows. 一方面,肯定无法在Windows上使用stdint.h进行编程。 On the other though, size of eg short isn't clear on the first batch of an eye. 但是,另一方面, short眼的大小在第一批眼睛上还不清楚。 And it's even more intuitive to write int8_t instead of char ... 而且,编写int8_t而不是char甚至更加直观。

Does C++ standard guarantee, that sizes of standard types will be unchanged in future? C ++标准是否保证标准类型的大小将来不会改变?

First off, Microsoft's implementation does support <stdint.h> . 首先,Microsoft的实现确实支持 <stdint.h>

Use the appropriate type for what you're doing. 为您的工作使用适当的类型。

If you need, for example, an unsigned type that's exactly 16 bits wide with no padding bits, use uint16_t , defined in <stdint.h> . 例如,如果您需要一个正好16位宽且没有填充位的无符号类型,请使用在<stdint.h>定义的uint16_t

If you need an unsigned type that's at least 16 bits wide, you can use uint_least16_t , or uint_fast16_t , or short , or int . 如果需要宽度至少为 16位的无符号类型,则可以使用uint_least16_tuint_fast16_tshortint

You probably don't need exact-width types as often as you think you do. 您可能不需要像您想的那样经常使用精确宽度类型。 Very often what matters is not the exact size of a type, but the range of values it supports. 通常,重要的不是类型的确切大小,而是它支持的值的范围。 But exact representation is important when you're interfacing to some externally defined data format. 但是,当您连接某些外部定义的数据格式时,精确的表示很重要。 In that case, you should already have declarations that tell you what types to use. 在这种情况下,您应该已经有声明,告诉您要使用的类型。

There are specific requirements on the ranges of the predefined types: char is at least 8 bits, short and int are at least 16 bits, long is at least 32 bits, and long long is at least 64 bits. 对预定义类型的范围有特定要求: char至少为8位, shortint至少为16位, long至少为32位, long long至少为64位。 Also, short is at least as wide as char , int is at least as wide as short , and so forth. 另外, short至少与char一样宽, int至少与short一样宽,依此类推。 (The standard specifies minimum ranges, but the minimum sizes can be derived from the ranges and the fact that a binary representation is required.) (标准指定了最小范围,但是最小大小可以从范围和需要二进制表示的事实中得出。)

Note that <stdint.h> is a C header. 请注意, <stdint.h>是C头。 If you #include it in a C++ program, the type names will be imported directly into the global namespace, and may or may not also be imported into the std namespace. 如果您#include它在C ++程序中,则类型名称将直接导入到全局名称空间中,并且可能会可能不会导入到std名称空间中。 If you #include <cstdint> , then the type names will be imported into the std namespace, and may or may not also be imported into the global namespace. 如果您#include <cstdint> ,则类型名称将被导入到std名称空间中,并且可能会可能不会被导入到全局名称空间中。 Macro names such as UINT32_MAX are not in any namespace; 宏名称(例如UINT32_MAX不在任何名称空间中; they're always global. 他们始终是全球性的。 You can use either version of the header; 您可以使用任何版本的标题; just be consistent about using or not using the std:: prefix. 只是在使用或不使用std::前缀方面保持一致。

C++ standard does not specify much about sizes of integer types (such as int , long or char ). C ++标准对整数类型(例如intlongchar )的大小没有太多规定。 If you want to be sure, that certain type has fixed size across platforms, you can use C++11's Fixed-width integer types , which are standardized and guaranteed to have given size. 如果要确定某个类型在各个平台上的大小是固定的,则可以使用C ++ 11的“ 固定宽度整数”类型 ,这些类型是标准化的并且保证具有给定的大小。

To use them, #include <cstdint> . 要使用它们,请#include <cstdint>

Does C++ standard guarantee, that sizes of standard types will be unchanged in future? C ++标准是否保证标准类型的大小将来不会改变?

Not likely. 不见得。 On 8bit computers, sizes of integers types were different to what they are today. 在8位计算机上,整数类型的大小不同于今天的大小。 In the future, in 2042, with 1024-bit computers , I assume long long to be 1024-bit long. 将来,在2042年,对于1024位计算机 ,我假设long long为1024位。

However, we can be almost absolutely sure, that std::uint32_t will stay 32-bit long. 但是,我们几乎可以完全确定, std::uint32_t将保持32位长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM