<limits>和<climits>的等效性[英] Equivalence of <limits> and <climits>

本文是小编为大家收集整理的关于<limits>和<climits>的等效性的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

  1. 这是保证总是正确的:

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

    C ++标准对此有何评价?我找不到标准中明确陈述这一点的任何参考资料,但我一直在读到那是等效的.

  2. 不在C ++ 98标准中的C99类型的编译器(至少至少long long part)和C ++ 98中的C99类型呢?我不确定是否可以保证这总是正确的:

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

    这是一个合理的假设吗?

推荐答案

我的C ++ 2003标准副本说numeric_limits<>::max()和min()模板将返回值:

等于CHAR_MIN, SHRT_MIN, FLT_MIN, DBL_MIN,等.

等效于CHAR_MAX, SHRT_MAX, FLT_MAX, DBL_MAX,等等

但是,这些都在脚注中. ISO/IEC指令第3部分:" [脚注]不包含要求."尽管脚注或数字可能是要求.

其他推荐答案

应该保证第一个是正确的:std::numeric_limits<int>::max() == INT_MAX.

然而,对于长时间的未签名,没有保证,因为编译器/库不需要支持它们.但是...

如果您的编译器和Libs支持未签名的长时间长,则应该相等,因为无论您问如何,类型的限制都将是相同的.

是的,这是一个合理的假设.

其他推荐答案

显然不能保证C ++中的长长数据类型,因为长长的数据类型尚未在C ++中存在.他们的行为如何,如果它们存在何时,则由编译器,并应由编译器记录.

本文地址:https://www.itbaoku.cn/post/1552011.html

问题描述

  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?

推荐答案

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.

其他推荐答案

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.

其他推荐答案

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.