zoukankan      html  css  js  c++  java
  • std::max与max宏 混淆解决办法(转)

    Today I typed the following:

    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:

    #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:

    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.

    • Use alternative names defined in windows.h.
      int x = _cpp_max(i,j);
      int y = _cpp_min(i,j);
      This is not portable; only works on Windows.

    • Define NOMINMAX before including windows.h. This might break existing code that assumes NOMINMAX is not defined.

    • Don't use std::min and std::max. Instead use the tertiary operator like so:
      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 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.

    • Use std::min<int> and 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:
      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.
  • 相关阅读:
    Intellij Idea安装主题包
    Spring中bean的含义
    IntelliJ IDEA 中 右键运行时没有run;新建时,选项没有Java class的解决方法和具体解释
    Linux下修改profile后用户无法登陆的问题
    11 | 怎么给字符串字段加索引?
    09 | 普通索引和唯一索引,应该怎么选择?
    08 | 事务到底是隔离的还是不隔离的?
    07 | 行锁功过:怎么减少行锁对性能的影响?
    06 | 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
    05 | 深入浅出索引(下)
  • 原文地址:https://www.cnblogs.com/timesdaughter/p/5894930.html
Copyright © 2011-2022 走看看