“Uint32”、“int16”等;它们是标准的 C++ 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/911035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:55:17  来源:igfitidea点击:

"Uint32", "int16" and the like; are they standard c++?

c++

提问by Zoomulator

I'm quite new to c++, but I've got the hang of the fundamentals. I've come across the use of "Uint32" (in various capitalizations) and similar data types when reading other's code, but I can't find any documentation mentioning them. I understand that "Uint32" is an unsigned int with 32 bits, but my compiler doesn't. I'm using visual c++ express, and it doesn't recognize any form of it from what I can tell.

我对 C++ 很陌生,但我掌握了基础知识。在阅读其他人的代码时,我遇到了“Uint32”(各种大写形式)和类似数据类型的使用,但我找不到任何提及它们的文档。我知道“Uint32”是一个 32 位的无符号整数,但我的编译器没有。我正在使用visual c++ express,但从我所知,它无法识别任何形式的它。

Is there some compilers that reads those data types by default, or have these programmers declared them themselves as classes or #define constants?

是否有一些编译器默认读取这些数据类型,或者这些程序员将它们自己声明为类或 #define 常量?

I can see a point in using them to know exactly how long your integer will be, since the normal declaration seems to vary depending on the system. Is there any other pros or cons using them?

我可以看到使用它们来确切知道您的整数有多长的一点,因为正常声明似乎因系统而异。使用它们还有其他优点或缺点吗?

采纳答案by Dani van der Meer

Visual c++ doesn't support the fixed-width integer types, because it doesn't include support for C99. Check out the answers to my question on this subjectfor various options you have for using them.

Visual c++ 不支持固定宽度的整数类型,因为它不包括对 C99 的支持。查看我关于这个主题的问题的答案,了解您使用它们的各种选择。

回答by paxos1977

Unix platforms define these types in stdint.h, this is the preferred method of ensuring type sizing when writing portable code.

Unix 平台在stdint.h 中定义了这些类型,这是在编写可移植代码时确保类型大小的首选方法。

Microsoft's platforms do not define this header, which is a problem when going cross-platform. If you're not using Boost Integer Libraryalready, I recommend getting Paul Hsieh's portable stdint.himplementation of this header for use on Microsoft platforms.

微软的平台没有定义这个头,这在跨平台时是一个问题。如果您还没有使用Boost Integer Library,我建议您获取 Paul Hsieh对此标头的可移植 stdint.h实现,以便在 Microsoft 平台上使用。

Update:Visual Studio 2010 and later do define this header.

更新:Visual Studio 2010 及更高版本确实定义了此标头。

回答by Greg Rogers

The C99 header file stdint.h defines typedefs of this nature of the form uint32_t. As far as I know, standard C++ doesn't provide a cstdint version of this with the symbols in namespace std, but some compilers may, and you will typically be able to include the C99 header from C++ code anyways. The next version of C++ will provide the cstdint header.

C99 头文件 stdint.h 定义了这种形式的 typedef uint32_t。据我所知,标准 C++ 不提供带有命名空间 std 中的符号的 cstdint 版本,但某些编译器可能会,并且您通常可以从 C++ 代码中包含 C99 标头。下一版本的 C++ 将提供 cstdint 头文件。

You will often see code from other people who use non-standard forms of this theme, such as Uint32_tor Uint32or uint32etc. They typically just provide a single header that defines these types within the project. Probably this code was originally developed a long time ago, and they never bothered to sed replace the definitions out when C99 compilers became common.

你会经常看到谁使用非标准形式这一主题的,比如别人的代码Uint32_tUint32uint32等,他们通常只是提供了一个头定义这些类型的项目中。可能这段代码最初是很久以前开发的,当 C99 编译器变得普遍时,他们从不费心用 sed 替换定义。

回答by ChrisBD

The main reason for using them is that you then don't have to worry about any possible problems arising when switching between 64bit and 32bit OS.

使用它们的主要原因是您不必担心在 64 位和 32 位操作系统之间切换时可能出现的任何问题。

Also if you are interfacing to any legacy code that you new was destined for 32bit or even 16bit then it avoids potential problems there as well.

此外,如果您要连接任何新的用于 32 位甚至 16 位的遗留代码,那么它也可以避免潜在的问题。

回答by Mark Ransom

Try UINT32 for Microsoft.

试试适用于 Microsoft 的 UINT32。

The upper case makes it clear that this is defined as a macro. If you try to compile using a different compiler that doesn't already contain the macro, you can define it yourself and your code doesn't have to change.

大写清楚地表明这被定义为一个宏。如果您尝试使用尚未包含该宏的其他编译器进行编译,您可以自己定义它并且您的代码不必更改。

回答by Dave W. Smith

uint32 et al.are defined by macros. They solve a historic portability problem of there being few guarantees across platforms (back when there there more platform options than now) of how many bits you'd get when you asked for an int or a short. (One now-defunct C compile for the Mac provided 8-bit shorts!).

uint32等。由宏定义。它们解决了一个历史性的可移植性问题,即当您要求 int 或 short 时,几乎没有跨平台保证(当平台选项比现在更多时)您将获得多少位。(一个现已不复存在的用于 Mac 的 C 编译提供了 8 位 shorts!)。