c++ - cstdint typedef 是否可以绑定(bind)到某些实现特定类型 std::numeric_limits 不是专门用于?

标签 c++ c++11 numeric-limits cstdint

是否有可能,至少在理论上,cstdint typedef 绑定(bind)到某些实现特定类型 std::numeric_limits 不是专门用于?

根据http://www.cplusplus.com/reference/limits/numeric_limits ,让我引述一下,“[std::numeric_limits] 专用于每个基本算术类型,其成员描述类型 T 的属性。此模板不得专用于任何其他类型。

根据 http://en.cppreference.com/w/cpp/types/numeric_limits ,让我再次引用,“实现可以为特定于实现的类型提供 std::numeric_limits 的特化”。

可能”,cppreference 说。所以他们不必。

最后,根据 http://www.cplusplus.com/reference/cstdint ,标题中定义的 typedef“是基本整数类型或扩展整数类型的 typedef”。

因此,总而言之 - 似乎 cstdint typedef 可能绑定(bind)到扩展整数类型(无论它们是什么),它们不是基本整数类型(同样,无论它们是),因此可能与 std::numeric_limits 不兼容。这是正确的吗?

但是,我链接到的文档在一点上似乎有些不一致。 cplusplus.com 是否禁止 std::numeric_limits 不得专用于任何非基础算术类型,以反对 cppreference 允许 std::numeric_limits 可能专用于特定于实现的类型?当然,除非这些特定于实现的类型实际上 基本整数类型,在这种情况下,希望 std::numeric_limits 必须专用于所有 cstdint 类型定义。

文档让我很困惑。所以我在这里问我的问题:)

编辑。

根据 http://eel.is/c++draft/cstdint , cstdint 必须绑定(bind)到整数类型。并根据http://eel.is/c++draft/limits.numeric , "应为每种算术类型提供专门化,包括 float 和整数,包括 bool"。 整数类型算术类型,因此 std::numeric_limits 必须专用于 cstdint typedef 的理解是否正确?

最佳答案

特化,例如 std::numeric_limits<std::int_fast32_t>必须存在。

3.9.1/2:

There are five standard signed integer types: "signed char", "short int", "int", "long int", and "long long int". ... There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types.

3.9.1/3:

For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type.... Likewise, for each of the extended signed integer types there exists a corresponding extended unsigned integer type.... The standard and extended unsigned integer types are collectively called unsigned integer types.

3.9.1/7:

Types bool, char, char16_t, char32_t, wchar_t, and the signed and unsigned integer types are collectively called integral types. A synonym for integral type is integer type.

3.9.1/8:

Integral and floating types are collectively called arithmetic types. Specializations of the standard template std::numeric_limits (18.3) shall specify the maximum and minimum values of each arithmetic type for an implementation.

18.3.2.1/2:

Specializations [of numeric_limits] shall be provided for each arithmetic type, both floating point and integer, including bool.

18.4.1:

namespace std {
  typedef signed_integer_type int8_t;    // optional
  //...
  typedef unsigned_integer_type uint8_t; // optional
  //...
}

因此 <cstdint> 中定义的类型可能是扩展类型,但绝对是整数类型,因此必须具有相应的特化 std::numeric_limits .

此外,所有整数类型都是标准 (3.9) 中使用的“基本”类型,尽管并非所有类型都是标准类型。

关于c++ - cstdint typedef 是否可以绑定(bind)到某些实现特定类型 std::numeric_limits 不是专门用于?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31527219/

相关文章:

c++ - 将对象写入文件时应用程序崩溃

c++ - 是否保证 `std::list::splice(std::const_iterator pos, std::list&& other)`保留 `other`为空?

c++ - 为什么有些 STL 算法提供额外的 '_if' 函数而不是重载?

c++ - 获取按位不为零的无符号整数的最大值

integer - 为什么无符号n位整数的最大值2 ^ n-1而不是2 ^ n?

c++ - 2 的幂的模幂

c++ - 指针函数的深拷贝

c++ - 读取未知数量的字符串后跟 double 成一个 vector

c++ - 为什么 numeric_limit<T>::min/max 不是常量?

c++ - 显式函数特化的默认模板参数