为什么 std::uint32_t 与 uint32_t 不同? [英] Why is std::uint32_t different from uint32_t?

查看:177
本文介绍了为什么 std::uint32_t 与 uint32_t 不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 c++ 有点陌生,我有一个编码任务,其中很多文件已经完成,但我注意到 VS2012 似乎有以下语句的问题:

I'm a bit new to c++ and I have a coding assignment with a lot of files already done, but I noticed that VS2012 seems to have an issue with the following statement:

typedef std::uint32_t 标识符;

不过,好像改成

typedef uint32_t 标识符;

摆脱错误.没有包含,这是在头文件中.我注意到定义在 stdint.h 中.如果是这种情况,为什么这段代码在 VS 之外是可以接受的(即使用 g++ 正确编译)但在 VS 中是不可接受的?谁能解释一下?

gets rid of the error. There are no includes and this is in the header file. I noticed that the definition is in stdint.h. If that's the case, why is this code acceptable outside of VS (ie. compiles properly using g++) but is unacceptable in VS? Can anyone please explain this?

推荐答案

区别在于一个在命名空间内,另一个不在命名空间内.否则它们应该是相同的.第一个应该是 C 版本,第二个是 C++ 版本.在 C++11 之前,强制要求包含前缀版本而不是 C 标准库版本将所有 C 定义引入标准命名空间内.在 C++11 中,此限制已放宽,因为这并非总是可行的.

The difference is that one is inside a namespace and the other isn't. Otherwise they should be the same. The first is supposed to be the C version and the second is the C++ version. Before C++11 it was mandated that including the prefixed versions instead of the C standard library version brings in all C definitions inside the standard namespace. In C++11 this restriction has been relaxed as this is not always possible.

可能是您的编译器隐式定义了这种类型.在任何情况下,您都应该包含 cstdint 以使名称空间 std 中的版本可用(并且可能是全局名称空间中的版本).包括 stdint.h 应该只是使不合格的版本可用.

It could be that your compiler defines this type implicitly. In any case, you should include cstdint to make the version in the namespace std available (and possibly the one in the global namespace). Including stdint.h should just make the unqualified version available.

早期版本的 Visual Studio 没有这个头文件,所以这肯定会很麻烦.

Earlier version of Visual Studio shipped without this header, so this is bound to be troublesome.

由于所有这些疯狂,大多数人会依赖第三方实现,例如 boost/cstdint.hpp.

Due all this madness, most people will fall back on a third-party implementation such as boost/cstdint.hpp.

它们是相同的,用于相同的目的.通常:如果要使用 std 命名空间中的版本,请包含 cstdint.如果您想要全局命名空间中的那个,请包含 stdint.h.对于 C++,建议使用 std 命名空间中的那个.通常:始终包含您使用的内容,不要依赖其他标题,包括适合您的内容.

They are the same and serve the same purpose. As a rule: If you want to use the version in the std namespace, include cstdint. If you want the one in the global namespace, include stdint.h. For C++ it is recommended to use the one in the std namespace. As a rule: Always include what you use and don't rely on other headers including things for you.

这篇关于为什么 std::uint32_t 与 uint32_t 不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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