【发布时间】:2021-07-15 10:49:08
【问题描述】:
最近我发现浮点类型和整数类型在 C++ 中的处理方式完全不同。
示例 1 来自:Why are floating point types invalid template parameter types for template functions?
template <double x>
double func() {
return x;
}
无效,而int 有效。
示例 2 来自:How to initialize private static members in C++?
class foo
{
private:
static float const i = 42;
};
无效,而int 有效。
在第一个链接问题中,有一个 Versuch 的答案。它指出 像 float 和 double 这样的类型在 C++ 中没有定义的实现。但是,C++ 中的许多其他地方也是如此,尤其是整数类型(1 补码与 2 补码)。
因此,浮点数和整数被区别对待是否有更深层次的原因?
【问题讨论】:
-
只是我的两分钱:整数类型的 2 补码在 C++20 AFAIK 中成为强制性的。
-
@Scheff 很高兴知道,谢谢。
标签: c++ floating-point integer