C++ limits头文件的用法(numeric_limits)

 

初学C++的时候,对这个模板很陌生,不知道它到底是做什么用的,今天拿起《C++标准程序库》,出现了它的讨论,所以决定好好研究一番。

1. numeric_limits是什么?

(A)《C++标准程序库》:

[cpp]  view plain cop
  1. 一般来说,数值型别的极值是一个与平台相关的特性。C++标准程序库通过template numeric_limits提供这些极值,取代传统C语言,所采用的预处理常数。新的极值概念有两个优点,第一是提供更好的型别安全性,第二是程序员可借此写出一些template以核定这些极值。  

(B)MSDN

[cpp]  view plain copy
  1. The template class describes arithmetic properties of built-in numerical types.  
  2.   
  3. The header defines explicit specializations for the types wchar_tboolcharsigned char, unsigned charshort, unsigned shortint, unsigned intlong, unsigned longfloatdouble, and long double. For these explicit specializations, the member numeric_limits::is_specialized is true, and all relevant members have meaningful values. The program can supply additional explicit specializations. Most member functions of the class describe or test possible implementations of float.  
  4.   
  5. For an arbitrary specialization, no members have meaningful values. A member object that does not have a meaningful value stores zero (or false) and a member function that does not return a meaningful value returns Type(0).  
  6.   
  7.   
  8.   
  9. 上面的意思是说:  
  10.   
  11. 这个模板类描述了内建类型的数值属性。  
  12.   
  13. C++标准库显式地为wchar_tboolcharsigned char, unsigned charshort, unsigned shortint, unsigned intlong, unsigned longfloatdouble, and long double这些类型提供了特化。对于这些类型来说,is_specialized为true,并且所有的相关的成员(成员变量或成员函数)是有意义的。这个模板也提供其他的特化。大部分的成员函数可以用float型别来描述或测试。  
  14.   
  15. 对于一个任意的特化,相关的成员是没有意义的。一个没有意义的对象一般用0(或者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值