Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::uint32_t different from uint32_t?

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 identifier;

However, it seems that changing it to

typedef uint32_t identifier;

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?

like image 758
Lunyx Avatar asked Feb 14 '13 20:02

Lunyx


People also ask

What is the difference between uint32_t and UInt32?

uint32_t is standard, uint32 is not. That is, if you include <inttypes. h> or <stdint. h> , you will get a definition of uint32_t .

What does uint32_t mean in C++?

uint32_t is a numeric type that guarantees 32 bits. The value is unsigned, meaning that the range of values goes from 0 to 232 - 1.

Is uint32_t same as int?

uint32_t is an unsigned integer of 32 bits. Probably the same as unsigned int but not guaranteed to be so. size_t is the type used to specify the size of memory allocations and the underlying type for indexes in the standard library. uint32_t is used when you must have a 32 bit unsigned.

Where is uint32_t defined?

This type is defined in the C header <stdint. h> which is part of the C++11 standard but not standard in C++03.


1 Answers

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.

like image 155
pmr Avatar answered Sep 28 '22 06:09

pmr