< cstdint> vs< stdint.h> [英] <cstdint> vs <stdint.h>

查看:190
本文介绍了< cstdint> vs< stdint.h>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

stdint.h cstdint 有什么区别?

它们都可以在MSVC(Visual Studio 2010)和gcc-4.5.1中使用。两者都定义 intX_t / uintX_t 类型(其中 X 是字节的大小)。

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).


  • 如果两个标头中的基本原理相同(便携式类型),我必须做出哪些决定

stdint.h 定义哪个决定?每种类型都没有任何命名空间,则 cstdint 类型位于 std 命名空间中。

The stdint.h defines each type without any namespace, the cstdint types lies in the std namespace.


  • 是否有理由在 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?

cstdint 没有文件扩展名,并且使用 c 前缀, stdint.h 使用 .h 扩展名。

cstdint has no file extension and uses the c prefix, stdint.h uses the .h extension.


  • 此标头的命名约定是什么? c 前缀表示这是C库吗?
  • code> cstdint 中缺少文件扩展名是有原因的吗?

  • 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 in cstdint?

推荐答案

在C ++ 98中,最初的目的是在C ++中使用< cstdint> ,以避免污染全局名称空间(嗯,而不是< cstdint> ,仅在C ++ 11中添加,而< c *>

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 ++ 11批准了这种做法[*]。因此,您基本上有三个选择:

However, implementations persisted in putting the symbols into the global namespace anyway, and C++11 ratified this practice[*]. So, you basically have three options:


  • 使用< cstdint> 或完全限定您使用的每种整数类型,或者使用std :: int32_t; 等将纳入范围(讨厌,因为冗长,但这是正确的方法,就像C ++标准库中的任何其他符号)

  • 使用< stdint.h> (由于已弃用而稍差)

  • 使用< cstdint> 并假定您的实现会将符号放在全局命名空间中(非常糟糕,因为不能保证)。

  • Use <cstdint> and either fully qualify each integer type you use or else bring it into scope with using 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> 将符号放在全局名称空间中。您应该尝试使用第一个。第二个优点是,保证将内容放入全局名称空间中,而不是仅仅这样做。我认为这不是特别有用,但是如果您优先考虑的话,它可能会节省一些键入内容。

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 guaranteed to 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.

还有第四种选择, #include< cstdint> 后跟使用命名空间std; 有时是有用的,但是有些地方不应该使用命名空间std; 。不同的人对这些地方的位置会有不同的想法,但是在头文件的顶层比在cpp文件的顶层差,而比在有限范围内差。某些人根本不会使用命名空间std来编写

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.

[*]这意味着允许使用C ++标准头将内容放到全局名称空间中,但不是必需的。因此,您必须避免与这些符号冲突,但是由于它们可能不存在而实际上不能使用它们。基本上,C ++中的全局名称空间是一个雷区,请尽量避免使用它。有人可能会争辩说,该委员会已批准一种实现,其危害与将使用命名空间std; 放在头文件的顶层几乎一样有害-区别在于实现只对C标准库中的符号执行此操作,而使用名称空间std; 也会对仅C ++的符号执行此操作。 C标准中有一部分列出了保留的名称,以供将来在标准中添加。将这些名称也视为在C ++全局命名空间中保留也不是一个完全愚蠢的主意,但这不是必须的。

[*] 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.

这篇关于&lt; cstdint&gt; vs&lt; stdint.h&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆