类 std::numeric_limits 中的字段与方法C++
Field vs. Method in C++ class std::numeric_limits
为什么在模板类std::numeric_limits
C++中,digits
(和其他)被定义为类的(静态常量)字段,但min()
和max()
是方法,因为这些方法只返回一个垃圾值?
提前谢谢。
不允许在类主体中初始化非整数常量(例如:浮点数)。在C++11中,声明改为
...
static constexpr T min() noexcept;
static constexpr T max() noexcept;
...
我认为,为了保持与 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;
}
相关文章:
- 使用同一类中的方法重新分配字段时出现问题
- Protobuf中重复字段的问题.使用重复字段进行序列化/反序列化的更好方法是什么?
- 对象引用中的字段以不同的方法返回不同的值
- 在类中定义数组的方法和字段
- 有什么方法可以"factor out"公共字段以节省空间?
- 如何从具有多个对象/字段的类中调用方法函数
- C 将方法和字段带入外部范围,而无需明确的参考
- 使用静态方法初始化 const 类字段的做法是好是坏
- 从CSV文件中提取每个条目字段的最佳方法是什么
- 使用某些"specialized"字段调用继承的方法
- 修复警告"field a is not used"如果字段在配置中未使用的好方法
- 使用方法影响另一个对象的字段
- 调用方法,该方法修改字段,而使用该字段的另一个方法正在执行
- 安全的方法来为char []字段创建Getter
- 修改Boost多索引项的非索引字段的最佳方法:modify vs mutable
- C :初始化成员结构的静态字段的正确方法
- C++ - 有没有办法将字段\方法添加到使用"typedef"定义的类型?
- Qt的Wizard注册字段,setField和字段方法如何工作?
- 如何在C++中用更少的空间定义类字段/方法
- 这两个字段/方法的区别是什么?