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.

std::max 错误的更多相关文章

  1. std::max、std::min error C2589: “(”:“::”右边的非法标记,error C2059: 语法错误:“::”

    在VC++种同时包含头文件#include <windows.h>和#include <algorithm>后就会出现无法正常使用std标准库中的min和max模板函数,经过查 ...

  2. 为什么在球坐标系中,sinTheta2=std::max(T(0), 1 - cosTheta(w) * cosTheta(w));

    球坐标系中,计算sin2θ时,采用的是如下公式,感觉不理解为什么要搞一个max函数,直接1 - cosTheta(w) * cosTheta(w)不行吗,另外,即使要用max,max的第一个参数应该是 ...

  3. EF6 CodeFirst连接MySql 报nvarchar('max')错误解决办法

    1.在DBContext类加标签[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] 2.在Nuget控制台输入u ...

  4. std::min 与std::max 的 Compiler Error C2780

    代码 #include<iostream>#include <algorithm> // std::min#undef minint main(){ float a =15.0 ...

  5. [ c++] cmake 编译时 undefined reference to `std::cout' 错误的解决方案

    cmake ..  和 make 之后,出现如下错误 Linking CXX executable ../../../bin/ModuleTest CMakeFiles/ModuleTest.dir/ ...

  6. Qt5.3编译错误——call of overloaded ‘max(int int)’is ambiguous

    错误描述: 今天在使用Qt写一个C++函数模板的测试程序的时候,编译的时候,编译的时候出现如下错误: 错误描述为:在main函数中,进行函数max()重载时,出现(ambiguous)含糊的,不明确的 ...

  7. max 宏定义取消:error C2589: error C2059: 语法错误 : “::”

    原文链接:http://blog.csdn.net/danelumax2/article/details/9172465有修改! 一:关于Pcl和WIndef的冲突: 1. 错误输出 ./zlibra ...

  8. 高德地图引入库错误std::string::find_first_of(char const*, unsigned long, unsigned long) const"

    一:std:编译器错误解决 二:错误提示 "std::string::find_first_of(char const*, unsigned long, unsigned long) con ...

  9. PCL -语法错误:“::” error C2589: “(”:“::”右边的非法标记

    1.错误原因:系统函数与pcl中的max函数冲突导致的 2.两种解决办法: 1)错误中max和min函数用括号括起来,例如"std::Max"修改为“(std::Max)”. 2) ...

随机推荐

  1. mysql-master-ha 实现mysql master的高可用。

    常用的mysql 高可用有下面几种方案: 名称 原理 特点 mysqlmha Perl脚本对mysql master做心跳,master down了以后,选举new master   ,是要改代理层的 ...

  2. RTMP、RTSP、HTTP视频协议详解(附:直播流地址、播放软件)

  3. 【uoj#143】[UER #5]万圣节的数列 构造

    题目描述 给出一个的数列,将其重新排列,使得其等差子序列的数目最小.输出一种可能的排列后的数列. 题解 构造 那天和 EdwardFrog 讨论 bzoj2124 的构造时突然有的灵感,最后发现就是这 ...

  4. 谷歌钦定的编程语言Kotlin大揭秘

    第一时间关注程序猿(媛)身边的故事 谷歌钦定的编程语言Kotlin大揭秘 语法+高级特性+实现原理:移动开发者升职加薪宝典! 谷歌作为世界级的科技公司巨头,强悍的技术研发与创新能力使其一直是业界的楷模 ...

  5. 洛谷 P3205 [HNOI2010]合唱队 解题报告

    P3205 [HNOI2010]合唱队 题目描述 为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个人的身高为 ...

  6. 【agc019D】Shift and Flip

    Portal --> agc019D Description 给你一个\(A\)串一个\(B\)串(长度相等的两个\(01\)串),一次操作可以选择将\(A\)向左循环移动一位,将\(A\)向右 ...

  7. RabbitMQ 相关概念

    RabbitMQ 整体上是一个生产者与消费者模型,主要负责接收.存储和转发消息.可以把消息传递的过程想象成:当你讲一个包裹送到邮局,邮局会暂存并最终将邮件通过邮递员送到收件人的手上,RabbitMQ ...

  8. bzoj 4919 [Lydsy1706月赛]大根堆 set启发式合并+LIS

    4919: [Lydsy1706月赛]大根堆 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 599  Solved: 260[Submit][Stat ...

  9. npm安装socket.io时报错的解决方法(npm WARN enoent ENOENT: no such file or directory, open '/usr/local/nodejs/bin/package.json')

    执行 npm install socket.io安装时报错: [root@WEB node_modules]# npm install socket.ionpm WARN enoent ENOENT: ...

  10. NATS_07:NATS之top工具监控以及测量调优工具

    概述 你可以使用 nats-top 来实现类似于 linux 中 top 命令的实时监控 nats 服务: 可以使用 nats 提供的工具来进行针对性的调优. 安装nats-top $ go get ...