Alex B
Alex B

Reputation: 84892

Equivalence of <limits> and <climits>

  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?

Upvotes: 3

Views: 3053

Answers (4)

Michael Burr
Michael Burr

Reputation: 340306

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.

Upvotes: 4

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 248129

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.

Upvotes: 1

Matt J
Matt J

Reputation: 45213

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

Upvotes: 0

Evan Teran
Evan Teran

Reputation: 90483

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.

Upvotes: 2

Related Questions