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 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.

时间: 2024-08-16 02:15:18

std::max与max宏 混淆解决办法(转)的相关文章

std::min和自定义宏冲突解决办法

#include<algorithm> #include<iostream> #define min(a,b)!(b<a)?a:b int main(){ int a=1,b=2; std::cout<<(std::min)(a,b); return 0; } 加入一个括号,std::min就不会被宏替代了. std::min和自定义宏冲突解决办法

Windows 10 上安装 3D Studio Max 2016 报错的解决办法

在 Windows 10 上安装 3D Stuido Max 2016 报错,无法正常安装,查看日志是 VC 运行时安装错误,经过分析发现在 Windows 10 上已经有这些运行时并且版本比安装包中自带的还要高,所以安装总是失败,分析安装程序配套的 setup.ini 文件,发现里面有运行时安装的配置信息,并且可以忽略错误,因此修改 setup.ini 文件,然后就可以顺利进行安装了,修改后的 setup.ini 文件如所示 #============= VCREDIST Begin ====

error C2589: &#39;(&#39; : illegal token on right side of &#39;::&#39; 解决办法

坑爹微软Sucks Again. assimp 终于更新到了5.0.0并且支持GLTF2格式,包含动画正确解析,在viewer中也能看到正确结果,真他喵的不容易,然后拿来编译完到自己项目里用,就出这玩意了,神奇的是编译他自己的viewer就没问题? 不管那么多,百度一下说是std::min之类的问题,解决办法 https://blog.csdn.net/leifeng_soul/article/details/52611390 5,6年前就听说不要用std::min,max做跨平台的东西,再之前

关于python3.6上传文件时报错:HTTPSConnectionPool(host=&#39;httpbin.org&#39;, port=443): Max retries exceeded with url: /post (Caused by SSLError(SSLError(1, &#39;[SSL: CERTIFICATE_VERIFY_FAIL解决办法

第一个报错: 最近在练习post请求中上传文件时遇到了一个奇葩事情,两台电脑上写了一模一样的代码,一个运行正常,另一个一片红. 最后了解了一下原因以及解决办法.先记录下关键代码: files = {"files":(r"F:\test.txt","xixihaha")} #直接将目标文件内容xixihaha通过文件test.txt进行上传 r = requests.post(url,files=files)print(r.headers) #前边

Velocity宏定义的坑与解决办法

使用Velocity,当然就免不了要使用宏,或者说使用Velocity而不使用其宏,就相当于废了Velocity一半以上的武功,非常可惜的.  怎么使用Velocity的宏呢,才最大程度的发挥其作用但是又避免掉入其中的坑呢?且听悠然乱弹乱弹: 官方文档中,关于Macro是这么说的: #macro - Allows users to define a Velocimacro (VM), a repeated segment of a VTL template, as required Format

FrameWork中SQLServer数据源使用宏函数出错解决办法

环境:DW是SQLServer2008 建模工具:Cognos FrameWork Version:Cognos10.2 出现问题:在FM中创建查询主题,sql类型数据源,引入宏函数#CAMIDList()# 验证模型直接报错如下 解决办法,在#CAMIDList()# 函数外套用sq函数,如下所示 #sq(CAMIDList())# like '%u:cn='+user_id+'"%' OK问题解决 主要目的是把like前后的对象都转为字符串的值 然后进行like

error: unrecognized command line option &quot;-std=c11&quot; 解决办法

今天在安装php版本 grpc扩展的时候报错如下: cc1: error: unrecognized command line option "-std=c11" cc1: warnings being treated as errors cc1: error: unrecognized command line option "-Wno-parentheses-equality" 开始搜寻解决方案,经查,出现这个 编译错误的原因在g++ gcc 版本不够高. 解决

宏基4752g 开机进度条卡到75%左右,解决办法

起因:更新win10推送的更新补丁,失败自动回退.开机进度条只能走到75%,bios进不去,最后就卡在开机的logo.(还有其他人是win7直接升级win10,也出现了这种情况.)解决办法:重刷bios.重刷bios,我知道的有4种:1.直接在window系统下更新(系统已挂,此种情况行不通)2.盲刷,据说是使用u盘引导,按组合键(尝试了很久,用了很多组合键,皆无法成功)3.买编程器和夹子(解决此情况采用的的办法)4.只使用编程器和SOP8宽座(非到万不得已不使用,动手能力强的使用)我用的是第3

PDF编译出现错误解决办法————————【Badboy】

额 今天  在编译PDF时发现使用了一下STL中的z数值极限竟然编译不过, return GetRangeConstraint(value <= std::numeric_limits::max(), true); 出现如下错误: warning C4003: "max"宏的实参不足 error C2589: "(" : "::"右边的非法标记 google了一下,原来是需要把max用括号括起来避免和windows定义的宏混淆 return