• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 311, comments - 0, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            (搬運工)std::min and std::max

            Posted on 2012-01-06 16:40 點點滴滴 閱讀(1505) 評論(0)  編輯 收藏 引用 所屬分類: 02 編程語言
            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 files\microsoft visual studio 8\vc\include\xutility(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 files\microsoft visual studio 8\vc\include\xutility(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.
            久久免费视频1| 狠狠色丁香久久婷婷综合蜜芽五月 | 精品综合久久久久久88小说| 亚洲午夜无码AV毛片久久| 欧美精品久久久久久久自慰| 久久精品国产99国产电影网| 国产精品久久久久蜜芽| 久久精品国产亚洲av麻豆小说| 久久久久亚洲国产| 伊人久久综合热线大杳蕉下载| 久久人人爽人人爽人人AV东京热 | 久久久亚洲欧洲日产国码aⅴ| 91久久精品国产成人久久| 狠狠综合久久综合88亚洲| 色播久久人人爽人人爽人人片aV| 久久九九全国免费| 青青草原综合久久| 无码人妻久久一区二区三区免费 | 精品无码人妻久久久久久 | 久久国产精品一区二区| 亚洲欧美一级久久精品| 青青草原综合久久| 精品久久久久久国产91| 国产精品99久久久精品无码| 欧美色综合久久久久久| 18岁日韩内射颜射午夜久久成人| 久久香综合精品久久伊人| 久久亚洲电影| 久久久久久国产精品无码下载| 2021国产精品午夜久久| 久久亚洲av无码精品浪潮| 精品综合久久久久久88小说| 久久婷婷综合中文字幕| 久久九九全国免费| 九九热久久免费视频| 一级做a爰片久久毛片人呢| 久久精品一区二区| 国产成人99久久亚洲综合精品| 欧美精品九九99久久在观看| 色悠久久久久久久综合网 | 99国产欧美久久久精品蜜芽 |