Gre*_*g82 12 c++ oop numeric-limits
为什么std::numeric_limits在C++ 的模板类中,digits(和其他)被定义为类的(静态const)字段,但是min()并且max()是方法,因为这些方法只返回一个小的值?
提前致谢.
小智 7
不允许在类体中初始化非整数常量(例如:浮点).在C++ 11中,声明改为
...
static constexpr T min() noexcept;
static constexpr T max() noexcept;
...
Run Code Online (Sandbox Code Playgroud)
为了保持与C++ 98的兼容性,我认为保留了这些功能.
例:
struct X {
// Illegal in C++98 and C++11
// error: ‘constexpr’ needed for in-class initialization
// of static data member ‘const double X::a’
// of non-integral type
//static const double a = 0.1;
// C++11
static constexpr double b = 0.1;
};
int main () {
std::cout << X::b << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)