std::numeric_limits的一个使用注意事项

作者:朱金灿

来源:http://blog.csdn.net/clever101


       在C/C++11中,std::numeric_limits为模板类,在库编译平台提供基础算术类型的极值等属性信息,取代传统C语言所采用的预处理常数(具体参考:C++常用数值类型的值范围的宏定义)。其中使用例子如下:

#include   
#include 

/* reference: 
    http://www.cplusplus.com/reference/limits/numeric_limits/ 
    https://msdn.microsoft.com/en-us/library/c707ct0t.aspx 
*/  
int test_numeric_limits_1()  
{  
    std::cout << std::boolalpha;  
    std::cout << "Minimum value for int: " << std::numeric_limits::min() << std::endl;  
    std::cout << "Maximum value for int: " << std::numeric_limits::max() << std::endl;  
    std::cout << "int is signed: " << std::numeric_limits::is_signed << std::endl;  
    std::cout << "Non-sign bits in int: " << std::numeric_limits::digits << std::endl;  
    std::cout << "int has infinity: " << std::numeric_limits::has_infinity << std::endl;  
  
    std::cout << "Minimum value for float: " << std::numeric_limits::min() << std::endl; // min returns the smallest positive value the type can encode, not the lowest  
    std::cout << "Lowest value for float: " << std::numeric_limits::lowest() << std::endl; // the lowest value  
    std::cout << "Maximum value for float: " << std::numeric_limits::max() << std::endl;  
    std::cout << "float is signed: " << std::numeric_limits::is_signed << std::endl;  
    std::cout << "Non-sign bits in float: " << std::numeric_limits::digits << std::endl;  
    std::cout << "float has infinity: " << std::numeric_limits::has_infinity << std::endl;  
  
    std::cout << "Minimum value for unsigned short: " << std::numeric_limits::min() << std::endl;  
    std::cout << "Maximum value for unsigned short: " << std::numeric_limits::max() << std::endl;  
  
    std::cout << "is_specialized(float): " << std::numeric_limits::is_specialized << std::endl;  
    std::cout << "is_integer(float): " << std::numeric_limits::is_integer << std::endl;  
    std::cout << "is_exact(float): " << std::numeric_limits::is_exact << std::endl;  
    std::cout << "is_bounded(float): " << std::numeric_limits::is_bounded << std::endl;  
    std::cout << "is_modulo(float): " << std::numeric_limits::is_modulo << std::endl;  
    std::cout << "is_iec559(float): " << std::numeric_limits::is_iec559 << std::endl;  
    std::cout << "digits10(float): " << std::numeric_limits::digits10 << std::endl;  
    std::cout << "radix(float): " << std::numeric_limits::radix << std::endl;  
    std::cout << "min_exponent(float): " << std::numeric_limits::min_exponent << std::endl;  
    std::cout << "max_exponent(float): " << std::numeric_limits::max_exponent << std::endl;  
    std::cout << "min_exponent10(float): " << std::numeric_limits::min_exponent10 << std::endl;  
    std::cout << "max_exponent10(float): " << std::numeric_limits::max_exponent10 << std::endl;  
    std::cout << "epsilon(float): " << std::numeric_limits::epsilon() << std::endl;  
    std::cout << "round_style(float): " << std::numeric_limits::round_style << std::endl;  
  
    std::cout << "The smallest nonzero denormalized value for float: "  
        << std::numeric_limits::denorm_min()<< std::endl;  
    std::cout << "The difference between 1 and the smallest value greater than 1 for float: "  
        << std::numeric_limits::epsilon()<< std::endl;  
    std::cout << "Whether float objects allow denormalized values: "  
        << std::numeric_limits::has_denorm << std::endl;  
    std::cout << "Whether float objects can detect denormalized loss: "  
        << std::numeric_limits::has_denorm_loss << std::endl;  
    std::cout << "Whether float objects have quiet_NaN: "  
        << std::numeric_limits::has_quiet_NaN << std::endl;  
    std::cout << "Whether float objects have a signaling_NaN: "  
        << std::numeric_limits::has_signaling_NaN << std::endl;  
    std::cout << "The base for type float is:  "  
        << std::numeric_limits::radix << std::endl;  
    std::cout << "The maximum rounding error for type float is:  "  
        << std::numeric_limits::round_error() << std::endl;  
    std::cout << "The rounding style for a double type is: "  
        << std::numeric_limits::round_style << std::endl;  
    std::cout << "The signaling NaN for type float is:  "  
        << std::numeric_limits::signaling_NaN() << std::endl;  
    std::cout << "Whether float types can detect tinyness before rounding: "  
        << std::numeric_limits::tinyness_before << std::endl;  
    std::cout << "Whether float types have implemented trapping: "  
        << std::numeric_limits::traps << std::endl;  
  
    return 0;  
} 

         但是在使用std::numeric_limits时经常会出现一个编译错误:

1>main.cpp(473):error C2059: 语法错误:“::”

1>  main.cpp(474): warning C4003: “min”宏的实参不足

 

      网上查了一下资料,了解了这个错误的原因:windows.h中定义了min宏,中也有这个定义,两者冲突了。windows.h中Microsoft定义了min/max两个宏,< limits >中定义的min/max两个宏虽然在自己的命名空间中,可还是被“污染”了。因此要避免冲突,就得先屏蔽windows.h中定义的min宏,具体做法是在所有包含头文件前面(注意是所有)增加下面的预处理语句:

#ifndef NOMINMAX                      
    #define NOMINMAX
#endif

参考文献:

1. C++/C++11中std::numeric_limits的使用

2. opencv编程解决warning C4003: “max”宏的实参不足

你可能感兴趣的:(C/C++)