|
|
Basic types | |||||||||||||||||||||
Fixed width integer types(C++11) | |||||||||||||||||||||
Fixed width floating-point types(C++23) | |||||||||||||||||||||
| |||||||||||||||||||||
Numeric limits | |||||||||||||||||||||
C numeric limits interface | |||||||||||||||||||||
Runtime type information | |||||||||||||||||||||
|
Static constants | ||||
(C++11) | ||||
numeric_limits::traps | ||||
Static member functions | ||||
(C++11) | ||||
Helper types | ||||
staticconstbool traps; | (until C++11) | |
staticconstexprbool traps; | (since C++11) | |
The value ofstd::numeric_limits<T>::traps istrue for all arithmetic typesT
that have at least one value at the start of the program that, if used as an argument to an arithmetic operation, will generate atrap.
Contents |
T | value ofstd::numeric_limits<T>::traps |
/* non-specialized */ | false |
bool | false |
char | usuallytrue |
signedchar | usuallytrue |
unsignedchar | usuallytrue |
wchar_t | usuallytrue |
char8_t(since C++20) | usuallytrue |
char16_t(since C++11) | usuallytrue |
char32_t(since C++11) | usuallytrue |
short | usuallytrue |
unsignedshort | usuallytrue |
int | usuallytrue |
unsignedint | usuallytrue |
long | usuallytrue |
unsignedlong | usuallytrue |
longlong(since C++11) | usuallytrue |
unsignedlonglong(since C++11) | usuallytrue |
float | usuallyfalse |
double | usuallyfalse |
longdouble | usuallyfalse |
On most platforms integer division by zero always traps, andstd::numeric_limits<T>::traps istrue for all integer types that support the value0. The exception is the typebool: even though division byfalse traps due to integral promotion frombool toint, it is the zero-valuedint that traps. Zero is not a value of typebool.
On most platforms, floating-point exceptions may be turned on and off at run time (e.g.feenableexcept() on Linux or_controlfp on Windows), in which case the value ofstd::numeric_limits<T>::traps for floating-point types reflects the state of floating-point trapping facility at the time of program startup, which isfalse on most modern systems. An exception would be aDEC Alpha program, where it istrue if compiled without-ieee
.
#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';}
Possible output:
// GCC output:bool: traps = truechar: traps = truechar16_t: traps = truelong: traps = truefloat: traps = false // Clang output:bool: traps = falsechar: traps = truechar16_t: traps = truelong: traps = truefloat: traps = false
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 497 | C++98 | it was unclear what is returned if trapping is enabled or disabled at runtime | returns the enable status at the start of the program |
Floating-point environment | |
[static] | identifies floating-point types that detect tinyness before rounding (public static member constant)[edit] |
[static] | identifies the floating-point types that detect loss of precision as denormalization loss rather than inexact result (public static member constant)[edit] |