numeric_limit提供数据类型信息,比如最大/最小值,有符号,是否有限等等,STL库已经具现化(实例化)char, bool, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, float, double和long double. 自定义类也可以通过具现化该类提供相关信息。
- #include <limits>
- long maxLong = std::numeric_limits<COLORREF>::max();
会出现编译错误
error C2589: '(' : illegal token on right side of '::'
error C2143: syntax error : missing ';' before '::'
错误的原因是编译器认为是这个max是winDef.h中的max宏。
- #ifndef max
- #define max(a,b) (((a) > (b)) ? (a) : (b))
- #endif
解决办法
1、修改语句
- #include <limits>
- long maxLong = (std::numeric_limits<long>::max)());
2、 undef windows max宏
- #ifdef max
- #undef max
- std::numeric_limits<COLORREF>::max();
- #endif
3、如果觉得1,2麻烦,也可以直接使用INT_MAX,LLONG_MAX。