为什么 std::numeric_limits<T>::max() 是一个函数?
在 C++ 标准库 中,值 std::numeric_limits<T>::max()
被指定为函数.特定类型的其他属性以常量形式给出(如std::numeric_limits<T>::is_signed
).所有 T
类型的常量都作为函数给出,而所有其他常量都作为常量值给出.
In the C++ Standard Library the value std::numeric_limits<T>::max()
is specified as a function. Further properties of a specific type are given as constants (likestd::numeric_limits<T>::is_signed
). All constants that are of type T
are given as functions, whereas all other constants are given as, well, constant values.
这背后的原理是什么?
推荐答案
扩展 Neil 的话,std::numeric_limit
To expand on Neil's remark, std::numeric_limit<T>
is available for any number type including floating point numbers, and if you dig through the comp.lang.c++ thread, you'll see the mention that it might not be possible to define the static variables for floating point values.
因此,为了保持一致性,他们决定将积分和浮点都放在方法后面.
So, for consistency they decided to put both integral and floating points behind methods.
它会随着 C++0x 而改变,所以有希望.
It will change with C++0x, so there's hope.
相关文章