std::numeric_limits<T>::traps
来自cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
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 输出:
bool: traps = true
char: traps = true
char16_t: traps = true
long: traps = true
float: traps = false
// Clang 输出:
bool: traps = false
char: traps = true
char16_t: traps = true
long: traps = true
float: traps = false
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 497 | C++98 | 不明确在运行时启用或禁用陷阱后返回的值 | 返回在程序启动时的启用状态 |
参阅
| 浮点环境 | |
[静态] |
鉴别检测舍入前是否非正规的浮点数类型 (公开静态成员常量) |
[静态] |
鉴别浮点数类型是否检测精度损失为非正规损失,而非不准确结果 (公开静态成员常量) |