Why is std :: uint32_t different from uint32_t?

I'm a little new to C ++, and I have an encoding purpose with a lot of files already done, but I noticed that VS2012 seems to have a problem with the following statement:

typedef std::uint32_t identifier;

However, it seems that his change to

typedef uint32_t identifier;

gets rid of the error. There are no inclusions, and this is in the header file. I noticed that the definition is in stdint.h. If so, why is this code valid outside of VS (i.e. it compiles correctly using g ++) but is not acceptable in VS? Can someone explain this?

+8
c ++ visual-studio-2012
source share
2 answers

The difference is that it is inside the namespace, while the other is not. Otherwise, they must be the same. The first should be a C version, and the second a C ++ version. Before C ++ 11, it was pointed out that including prefix versions instead of the standard version of the C library includes all C definitions inside the standard namespace. In C ++ 11, this restriction has been relaxed, as it is not always possible.

Perhaps your compiler implicitly defines this type. In any case, you must enable cstdint to make the version in the std available (and possibly the one contained in the global namespace). Enabling stdint.h should only make an unqualified version available.

An earlier version of Visual Studio comes without this header, so this is inevitable.

Because of all this frenzy, most people will give up third-party implementations like boost/cstdint.hpp .

Edit: They are the same and serve the same purpose. Typically: if you want to use the version in the std , enable cstdint . If you want to use this in the global namespace, include stdint.h . For C ++, it is recommended to use one in the std . As a rule of thumb: always include what you use, and do not rely on other headers, including things for you.

+10
source share

uint32_t (aka ::uint32_t i.e. the one in the global namespace) is declared in <stdint.h> . This header can also declare it in the std , like std::uint32_t , but this is not required for this.

std::uint32_t (i.e. one in the std ) is declared in <cstdint> . This header can also declare it in the global namespace, like ::uint32_t , but this is not required.

If so, why is this code valid outside of VS (i.e. compiles correctly using g ++) but not acceptable in VS? Can someone explain this?

If you want to use std::uint32_t , then you must #include <cstdint> , or the code cannot compile. If it compiles with g ++, then probably some other header indirectly includes <cstdint> , but you should not rely on it, include the correct headers for the names you use.

+7
source share

All Articles