std::min and std::max
2016-02-25 17:33
330 查看
int t = (std::max)(timeout, lagtime);
Why did I put parentheses around std::max? Because windows.h defines (among other things) a max and a min macro. If you include windows.h the above code will not compile. For example the following:
之所以为std::max添加括号, 是因为在windows.h里面定义了"max"和"min"的宏。如果你的程序中包含了windows.h如下所示:
#include "windows.h" #include <algorithm> void foo() { int i = 5; int j = 7; int x = std::max(i,j); }
Will produce the following error with Visual Studio C++ 2005:
在VS05(及其他的版本)中就会出现这样的错误,
1>test.cpp(7) : error C2589: '(' : illegal token on right side of '::' 1>test.cpp(7) : error C2143: syntax error : missing ';' before '::'
There are a number of ways to work around windows.h defining these two macros.
有多种该方法可以使用windows.h里面定义的这两个宏.
Use alternative names defined in windows.h.
1 使用这两个函数在windows.h里面定义的别名
int x = _cpp_max(i,j); int y = _cpp_min(i,j);
This is not portable; only works on Windows.
这是不能跨平台的,只能在windows里面使用.
Define NOMINMAX before including windows.h. This might break existing code that assumes NOMINMAX is not defined.
在使用windows.h之前定义 "NOMINMAX" 宏,但是,这将导致已经以未定义此宏为前提而使用的函数出错.
Don’t use std::min and std::max. Instead use the tertiary operator like so:
不再使用std::min,std::max , 使用如下的三目运算符:
int x = i > j ? i : j; // max(i,j) int y = i < j ? i : j; // min(i,j)
This is portable but not as readable and more error prone.
这样虽可行,但是可读性会降低,再且更容易出错.
Use using statements to make the code portable:
使用Using声明
using std::min; using std::max; int x = max(i,j); int y = min(i,j);
This works but requires two more lines of code. You could also just use ‘using namespace std;’ but that might pull in more than you want.
这虽可行但是多写了两行代码, 我们也可以添加using namespace std; 但这样,会出现其他潜在的问题, 如打破命名空间.
Use std::min<int> and std::max<int>
使用std::min<int> 和 std::max<int>
int x = std::max<int>(i,j); int y = std::min<int>(i,j);
This requires you to specify the type. However in some cases this actually helps. For example: (这样的话你还需要指明变量类型,不过它也确实是可行的办法.如下:)
int i = 5; unsigned int j = 7; int x = (std::max)(i,j); int y = (std::min)(i,j);
Note the ‘unsigned’. Generates the following errors:(注意,"unsingned"如引发如下的错误)
1>test.cpp(7) : error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 2 provided (提供的参数过个数不正确) 1> c:program filesmicrosoft visual studio 8vcincludexutility(3190) : see declaration of 'std::max' 1>test.cpp(7) : error C2782: 'const _Ty &std::max(const _Ty &,const _Ty &)' : template parameter '_Ty' is ambiguous 1> c:program filesmicrosoft visual studio 8vcincludexutility(3182) : see declaration of 'std::max' 1> could be 'unsigned int' 1> or 'int'
By explicitly specifying type via <int> you remove the ambiguity.(使用显式说明的方式消除歧义)
Use (std::min) and (std::max)
int i = 5; int j = 7; int x = (std::max)(i,j); int y = (std::min)(i,j);
This works (as does the std::max<int>) because the C++ preprocessor requires ‘(‘ as the next preprocessing token following the macro name to preform the macro expansion.
如此可行是因为括号的存在规范了对应的宏的结合方式,以避免错误或者歧义.
相关文章推荐
- handler 复习
- 多线程互斥
- Bitmap对象在内存中的大小和转化为字节流的大小
- Javascript—XPATH技术解析XML
- 将EXCEL自定义时间格式转换为时间格式
- jackson依赖的jar包
- idapython让生活变得更美好:第五部分
- idapython让生活变得更美好:第四部分
- 笔记本电脑关闭小键盘(即打字按P出现星号键)
- 从TCP协议的原理来谈谈rst复位攻击
- Android应用开发中Fragment与Activity间通信示例讲解
- 校园网&openwrt记(二)应用的大体设计
- HTTP method POST is not supported by this URL
- PAT (Basic Level)1015. 德才论
- ANDROID安卓activity与fragment,activity之间互相传值(二)
- javascript 中的arguments.callee
- idapython让生活变得更美好:第二部分
- idapython让生活变得更美好:第三部分(条件断点)
- javaScript中call,apply,bind方法的总结。
- 获取当前月份的所有日期