C++ <cstdint> 与 <stdint.h>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13642827/
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
<cstdint> vs <stdint.h>
提问by PaperBirdMaster
What is the difference between stdint.h
and cstdint
?
stdint.h
和 和有cstdint
什么区别?
Both of them are available in MSVC (Visual Studio 2010) and gcc-4.5.1. Also both define the intX_t
/uintX_t
types (where X
is the size in bytes of the type).
它们都在 MSVC (Visual Studio 2010) 和 gcc-4.5.1 中可用。也都定义了intX_t
/uintX_t
类型(其中X
以字节为单位的类型大小)。
- If the rationale in both headers is the same (portable types), what decisions I must take to decide on one or the other?
- 如果两个标题中的基本原理相同(便携式类型),我必须做出哪些决定来决定其中一个?
The stdint.h
defines each type without any namespace, the cstdint
types lies in the std
namespace.
在stdint.h
没有任何命名空间中,定义了每种类型的cstdint
类型就在于std
命名空间。
- Is there any reason to include or to not include the defined types into the
std
namespace? What is different between the two headers?
- 是否有任何理由在
std
命名空间中包含或不包含定义的类型?这两个标题有什么不同?
cstdint
has no file extension and uses the c
prefix, stdint.h
uses the .h
extension.
cstdint
没有文件扩展名并使用c
前缀,stdint.h
使用.h
扩展名。
- What are the naming conventions for this headers? the
c
prefix indicates that this is a C library? there's a reason for the lack of file extension incstdint
?
- 此标头的命名约定是什么?该
c
前缀表示这是一个C库?缺少文件扩展名的原因是cstdint
什么?
回答by Steve Jessop
The original intention in C++98 was that you should use <cstdint>
in C++, to avoid polluting the global namespace (well, not <cstdint>
in particular, that's only added in C++11, but the <c*>
headers in general).
C++98 的初衷是你应该<cstdint>
在 C++ 中使用,以避免污染全局命名空间(好吧,不是<cstdint>
特别,它只在 C++11 中添加,但<c*>
一般是头文件)。
However, implementations persisted in putting the symbols into the global namespace anyway, and C++11 ratified this practice[*]. So, you basically have three options:
然而,实现仍然坚持将符号放入全局命名空间,C++11 批准了这种做法[*]。所以,你基本上有三个选择:
- Use
<cstdint>
and either fully qualify each integer type you use or else bring it into scope withusing std::int32_t;
etc (annoying because verbose, but it's the right way to do it just like for any other symbol in the C++ standard library) - Use
<stdint.h>
(slightly bad because deprecated) - Use
<cstdint>
and assume your implementation will put the symbols in the global namespace (very bad because not guaranteed).
- 使用
<cstdint>
并完全限定您使用的每个整数类型,或者将其带入using std::int32_t;
etc 的范围内(烦人,因为冗长,但它是正确的方法,就像 C++ 标准库中的任何其他符号一样) - 使用
<stdint.h>
(略差,因为已弃用) - 使用
<cstdint>
并假设您的实现会将符号放在全局命名空间中(非常糟糕,因为不能保证)。
In practice I suspect that an annoying large amount of code uses the last option, simply because it's easy to do by accident on an implementation where <cstdint>
puts the symbols in the global namespace. You should try to use the first. The second has one virtue, that it is guaranteedto put stuff in the global namespace instead of only maybe doing it. I don't think that's particularly useful, but it might save some typing if that's your priority.
在实践中,我怀疑大量令人讨厌的代码使用了最后一个选项,仅仅是因为在<cstdint>
将符号放在全局命名空间中的实现中很容易意外地做到这一点。您应该尝试使用第一个。第二个有一个优点,它保证将东西放在全局命名空间中,而不仅仅是可能这样做。我认为这不是特别有用,但如果这是您的首要任务,它可能会节省一些打字时间。
There's a fourth option, #include <cstdint>
followed by using namespace std;
which is sometimes useful but there are places that you shouldn't put the using namespace std;
. Different people will have different ideas where those places are, but "at top level in a header file" is worse than "at top level in a cpp file", which is worse than "in a limited scope". Some people never write using namespace std;
at all.
还有第四个选项,#include <cstdint>
后面的选项using namespace std;
有时很有用,但有些地方你不应该把using namespace std;
. 不同的人会对这些地方有不同的想法,但是“在头文件中的顶级”比“在cpp文件中的顶级”更糟糕,后者比“在有限范围内”更糟糕。有些人根本不写using namespace std;
。
[*] That means C++ standard headers are permitted to put stuff in the global namespace but not required to. So you have to avoid colliding with those symbols, but you can't actually use them because they might not be there. Basically, the global namespace in C++ is a minefield, try to avoid it. One might argue that the committee has ratified a practice by implementations that is nearly as harmful as sticking using namespace std;
at top level in a header file -- the difference being that the implementations only do it for symbols in the C standard library, whereas using namespace std;
does it for C++-only symbols too. There's a section in the C standard that lists names reserved for future additions to the standard. It's not a completely stupid idea to treat those names as reserved in the C++ global namespace too, but it's not essential.
[*] 这意味着允许 C++ 标准头文件将内容放在全局命名空间中,但不是必需的。所以你必须避免与这些符号发生冲突,但你实际上不能使用它们,因为它们可能不存在。基本上,C++ 中的全局命名空间是一个雷区,尽量避免它。有人可能会争辩说,委员会已经批准了一种实现的做法,这种做法几乎与坚持using namespace std;
头文件中的顶层一样有害——不同之处在于这些实现仅对 C 标准库中的符号执行此操作,而using namespace std;
对 C++ 执行此操作- 也只有符号。C 标准中有一个部分列出了为将来添加到标准中保留的名称。将这些名称也视为 C++ 全局命名空间中的保留名称并不是一个完全愚蠢的想法,但这并不是必需的。
回答by Alok Save
Including cstdint
imports the symbol names in std namespace and possiblyin Global namespace.
Including stdint.h
imports the symbol names in Global namespace and possiblyin std namespace.
包括cstdint
在 std 命名空间和可能在全局命名空间中导入符号名称。
包括stdint.h
在全局命名空间和可能在 std 命名空间中导入符号名称。
Features of C standard Library are also provided in the C++ Standard library and as a general naming convention they are pre-pended by an c to the corresponding names in C standard library.
C++ 标准库中也提供了 C 标准库的特性,作为一般命名约定,它们在 C 标准库中的相应名称前加了一个 c。
In C++, You should be using:
在 C++ 中,您应该使用:
#include <cstdint>
and fully qualify the symbol names you use with std::
while in C, You should use:
并完全限定您std::
在 C 中使用的符号名称,您应该使用:
#include <stdint.h>
Annex D (normative) Compatibility features [depr]states:
附件 D(规范性)兼容性特性 [depr]规定:
D.6 C standard library headers
D.6 C 标准库头文件
1 For compatibility with the C standard library and the C Unicode TR, the C++ standard library provides the 25 C headers, as shown in Table 151.
1 为了与 C 标准库和 C Unicode TR 兼容,C++ 标准库提供了 25 个 C 头文件,如表 151 所示。
Which include:
其中包括:
<assert.h> <float.h> <math.h> <stddef.h> <tgmath.h>
<complex.h> <inttypes.h> <setjmp.h> <stdio.h> <time.h>
<ctype.h> <iso646.h> <signal.h> <stdint.h> <uchar.h>
<errno.h> <limits.h> <stdarg.h> <stdlib.h> <wchar.h>
<fenv.h> <locale.h> <stdbool.h> <string.h> <wctype.h>
<assert.h> <float.h> <math.h> <stddef.h> <tgmath.h>
<complex.h> <inttypes.h> <setjmp.h> <stdio.h> <time.h>
<ctype.h> <iso646.h> <signal.h> <stdint.h> <uchar.h>
<errno.h> <limits.h> <stdarg.h> <stdlib.h> <wchar.h>
<fenv.h> <locale.h> <stdbool.h> <string.h> <wctype.h>
And further,
并进一步,
2 Every C header, each of which has a name of the form
name.h
, behaves as if each name placed in the standard library namespace by the correspondingcname header
is placed within the global namespace scope.It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).3 [ Example: The header
<cstdlib>
assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header<stdlib.h>
assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. —end example ]
2 每个 C 头文件,每个头文件都有一个形式为 的名称,其
name.h
行为就好像由相应的标准库名称空间中的每个名称放置在cname header
全局名称空间范围内。未指定这些名称是否首先在命名空间 std 的命名空间范围 (3.3.6) 内声明或定义,然后通过显式 using 声明 (7.3.3) 注入全局命名空间范围。3 [ 示例:标题
<cstdlib>
肯定会在命名空间 std 中提供其声明和定义。它还可以在全局命名空间内提供这些名称。头文件<stdlib.h>
确实在全局命名空间中提供了相同的声明和定义,就像在 C 标准中一样。它还可以在命名空间 std 中提供这些名称。—结束示例]
回答by hate-engine
cstdint
is C++11 header,stdint.h
is C99 header (C and C++ are different languages!)MSVC 2008 contains neither
stdint.h
norcstdint
.Implementations of
cstdint
are mostly simply#include <stdint.h>
with some namespace/language fixes.
cstdint
是 C++11 头文件,stdint.h
是 C99 头文件(C 和 C++ 是不同的语言!)MSVC 2008 既不包含
stdint.h
也不包含cstdint
.的实现
cstdint
大多只是简单#include <stdint.h>
的一些命名空间/语言修复。