tags:

views:

972

answers:

4

In C99, I include stdint.h and that gives me UINT32_MAX as well as uint32_t. However, in C++ the UINT32_MAX gets defined out. I can define __STDC_LIMIT_MACROS before including stdint.h, but this doesn't work if someone is including my header after already including stdint.h themselves.

So in C++, what's the standard way of finding out the maximum value representable in a uint32_t?

+7  A: 

std::numeric_limits<T>::max() defines the maximum value for type T.

Pete Kirkham
std::numeric_limits<T>::max() :-)
Jasper Bekkers
thanks.
Pete Kirkham
+12  A: 

Well, I don't know about uint32_t but for the fundamental types (bool, char, signed char, unsigned char, wchar_t, short, unsigned short, int, unsigned int, long, unsigned long, float, double and long double) you should use the numeric_limits templates.

cout << "Minimum value for int: " << numeric_limits<int>::min() << endl;
cout << "Maximum value for int: " << numeric_limits<int>::max() << endl;

If uint32_t is a #define of one of the above than this code should work out of the box

    cout << "Maximum value for uint32_t: " << numeric_limits<uint32_t>::max() << endl;
Glen
+3  A: 

Well, uint32_t will always be 32 bit, and always be unsigned, so you can safely define it manually:

#define UINT32_MAX  (0xffffffff)

You can also do

#define UINT32_MAX  ((uint32_t)-1)
Lior Kogan
A: 

You may be able to eliminate the #include order problems by changing your build process to define the __STDC_LIMIT_MACROS symbol on the compiler command line instead:

cxx -D__STDC_LIMIT_MACROS ...

Of course, you would still have trouble if a header #undefs this symbol.

Also, the authors of the standard library implementation that you are using might not have intended for users to set that particular symbol; there might be a compiler flag or a different symbol that users are intended to use to enable C99 types in C++.

bk1e