Why is std::uint32_t different from uint32_t?

前端 未结 2 1527
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 20:35

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:

相关标签:
2条回答
  • 2021-02-13 21:20

    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.

    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.

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

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

    Edit: 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.

    0 讨论(0)
  • 2021-02-13 21:39

    uint32_t (aka ::uint32_t i.e. the one in the global namespace) is declared in <stdint.h>. That header might also declare it in namespace std, as std::uint32_t, but it's not required to do so.

    std::uint32_t (i.e. the one in namespace std) is declared in <cstdint>. That header might also declare it in the global namespace, as ::uint32_t, but it's not required to do so.

    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?

    If you want to use std::uint32_t then you must #include <cstdint>, or the code may not compile. If it does compile with G++ then presumably some other header indirectly includes <cstdint> but you shouldn't rely on that, include the right headers for the names you use.

    0 讨论(0)
提交回复
热议问题