std::min and std::max

来源:互联网 发布:经常性失眠的原因 知乎 编辑:程序博客网 时间:2024/06/01 08:36
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 ambiguous1>        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.

  • 如此可行是因为括号的存在规范了对应的宏的结合方式,以避免错误或者歧义.

0 0
原创粉丝点击