std::numeric_limits
提供了 2 个互斥的常量:
is_integer
: “true
对于所有整数算术类型T
”is_exact
: “true
对于所有使用精确表示的算术类型T
”
是否存在非精确整数类型的可能性?在这里允许做什么?
在我知道我是否正在处理精确数字的所有模板中,我使用了 is_integer
,我现在是否还需要添加对 is_exact
的检查?
最佳答案
来自 is_exact
cppreference 页面:
Notes
While all fundamental types T for which
std::numeric_limits<T>::is_exact==true
are integer types, a library may define exact types that aren't integers, e.g. a rational arithmetics type representing fractions.
而且,正如@Holt 所提到的,该标准也对其进行了描述:
21.3.4.1 numeric_limits members [numeric.limits.members]
static constexpr bool is_exact;
true if the type uses an exact representation. All integer types are exact, but not all exact types are integer. For example, rational and fixed-exponent representations are exact but not integer.
关于c++ - 为什么标准同时提供了 is_integer 和 is_exact?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48227296/