Equivalence of <limits> and <climits>

限于喜欢 提交于 2019-12-14 01:37:47

问题


  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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/653150/equivalence-of-limits-and-climits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!