6

std::numeric_limits provides 2 constants that are mutually exclusive:

  • is_integer : "true for all integer arithmetic types T"

  • is_exact: "true for all arithmetic types T that use exact representation"

Is there the possibility of a non-exact integral type? What is trying to be allowed for here?

In all my templates where I to know if I am dealing with precise numbers, I used is_integer, do I need to go add a check for is_exact as well now?

4

1 回答 1

13

cppreference页面:is_exact

笔记

虽然所有基本类型 T std::numeric_limits<T>::is_exact==true都是整数类型,但库可以定义不是整数的确切类型,例如表示分数的有理算术类型。


而且,正如@Holt 所提到的,该标准也对其进行了描述:

21.3.4.1 numeric_limits 成员 [numeric.limits.members]

static constexpr bool is_exact;

如果类型使用精确表示,则为 true。所有整数类型都是精确的,但并非所有精确类型都是整数。例如,有理和固定指数表示是精确的但不是整数。

于 2018-01-12T13:32:08.297 回答