views:

340

answers:

4
  1. Is this guaranteed to be always true:

    std::numeric_limits<int>::max() == INT_MAX
    

    What does C++ standard say about it? I could not find any reference in the standard that would explicitly state this, but I keep reading that those should be equivalent.

  2. What about C99 types that are not in C++98 standard for compilers that implement both C99 (at least long long part) and C++98? I am not sure whether there is any guarantee that this always holds true:

    std::numeric_limits<unsigned long long>::max() == ULLONG_MAX
    

    Is this a reasonable assumption?

+2  A: 

The first should be guaranteed to be true: std::numeric_limits<int>::max() == INT_MAX.

However, for unsigned long long, there are no guarantees since the compiler/libraries are not required to support them. But...

If your compiler and libs support unsigned long long, it should be equal since the limits for the types will be the same regardless of how you ask.

yes, it is this a reasonable assumption.

Evan Teran
+3  A: 

My copy of the C++ 2003 standard says that the numeric_limits<>::max() and min() templates will return values:

Equivalent to CHAR_MIN, SHRT_MIN, FLT_MIN, DBL_MIN, etc.

Equivalent to CHAR_MAX, SHRT_MAX, FLT_MAX, DBL_MAX, etc

However, those are in footnotes. ISO/IEC Directives Part 3: "[Footnotes] shall not contain requirements." Though footnotes to tables or figures may be requirements.

Michael Burr
To be pedantic, footnotes are not part of the standard.
dirkgently
@dirkgently - You're right (though this is a pain to find). It's in ISO/IEC Directives Part 3: "[Footnotes] shall not contain requirements." Though footnotes to tables or figures may be requirements.
Michael Burr
Unfortunately it seems to be the only place in the standard mentioning this.
Alex B
A: 

While, to my knowledge, it is not part of either standard, it is surely a reasonable assumption, particularly if you have compilers from the same suite or vendor. For instance, G++ 4.3 simply uses the #defined values from <limits> in <climits>.

Matt J
+1  A: 

There are obviously no guarantees for long long datatypes in C++, since long long datatypes do not yet exist in C++. How they behave, if and when they exist, is up to the compiler, and should be documented by the compiler.

jalf