std::numeric_limits<T>::traps
来自 cppreference.cn
< cpp | types | numeric limits
static const bool traps; |
(直到 C++11) | |
static constexpr bool traps; |
(自 C++11 起) | |
std::numeric_limits<T>::traps 的值对于所有算术类型 T
而言为 true,这些类型在程序开始时至少有一个值,如果用作算术运算的参数,将生成陷阱。
目录 |
[编辑] 标准特化
T
|
std::numeric_limits<T>::traps 的值 |
/* 非特化 */ | false |
bool | false |
char | 通常 true |
signed char | 通常 true |
unsigned char | 通常 true |
wchar_t | 通常 true |
char8_t (自 C++20 起) | 通常 true |
char16_t (自 C++11 起) | 通常 true |
char32_t (自 C++11 起) | 通常 true |
short | 通常 true |
unsigned short | 通常 true |
int | 通常 true |
unsigned int | 通常 true |
long | 通常 true |
unsigned long | 通常 true |
long long (自 C++11 起) | 通常 true |
unsigned long long (自 C++11 起) | 通常 true |
float | 通常 false |
double | 通常 false |
long double | 通常 false |
[编辑] 注意
在大多数平台上,整数除以零总是会产生陷阱,并且对于所有支持值 0 的整数类型,std::numeric_limits<T>::traps 为 true。例外是类型 bool:即使除以 false 会由于从 bool 到 int 的整型提升而产生陷阱,但它是值为零的 int 产生陷阱。零不是 bool 类型的值。
在大多数平台上,浮点异常可以在运行时打开和关闭(例如 Linux 上的 feenableexcept() 或 Windows 上的 _controlfp),在这种情况下,浮点类型的 std::numeric_limits<T>::traps 的值反映了程序启动时浮点陷阱设施的状态,这在大多数现代系统上是 false。一个例外是 DEC Alpha 程序,如果在没有 -ieee
的情况下编译,则为 true。
[编辑] 示例
运行此代码
#include <iostream> #include <limits> int main() { std::cout << std::boolalpha << "bool: traps = " << std::numeric_limits<bool>::traps << '\n' << "char: traps = " << std::numeric_limits<char>::traps << '\n' << "char16_t: traps = " << std::numeric_limits<char16_t>::traps << '\n' << "long: traps = " << std::numeric_limits<long>::traps << '\n' << "float: traps = " << std::numeric_limits<float>::traps << '\n'; }
可能的输出
// GCC output: bool: traps = true char: traps = true char16_t: traps = true long: traps = true float: traps = false // Clang output: bool: traps = false char: traps = true char16_t: traps = true long: traps = true float: traps = false
[编辑] 缺陷报告
以下行为变更的缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布的行为 | 正确的行为 |
---|---|---|---|
LWG 497 | C++98 | 不清楚在运行时启用或禁用陷阱时会返回什么 已启用或禁用 |
返回程序启动时的启用状态 在程序启动时 |
[编辑] 参见
浮点环境 | |
[静态] |
标识在舍入前检测到极小的浮点类型 (公共静态成员常量) |
[静态] |
标识将精度损失检测为非正常化损失而不是不精确结果的浮点类型 (公共静态成员常量) |