std::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.
std::max 错误的更多相关文章
- std::max、std::min error C2589: “(”:“::”右边的非法标记,error C2059: 语法错误:“::”
在VC++种同时包含头文件#include <windows.h>和#include <algorithm>后就会出现无法正常使用std标准库中的min和max模板函数,经过查 ...
- 为什么在球坐标系中,sinTheta2=std::max(T(0), 1 - cosTheta(w) * cosTheta(w));
球坐标系中,计算sin2θ时,采用的是如下公式,感觉不理解为什么要搞一个max函数,直接1 - cosTheta(w) * cosTheta(w)不行吗,另外,即使要用max,max的第一个参数应该是 ...
- EF6 CodeFirst连接MySql 报nvarchar('max')错误解决办法
1.在DBContext类加标签[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] 2.在Nuget控制台输入u ...
- std::min 与std::max 的 Compiler Error C2780
代码 #include<iostream>#include <algorithm> // std::min#undef minint main(){ float a =15.0 ...
- [ c++] cmake 编译时 undefined reference to `std::cout' 错误的解决方案
cmake .. 和 make 之后,出现如下错误 Linking CXX executable ../../../bin/ModuleTest CMakeFiles/ModuleTest.dir/ ...
- Qt5.3编译错误——call of overloaded ‘max(int int)’is ambiguous
错误描述: 今天在使用Qt写一个C++函数模板的测试程序的时候,编译的时候,编译的时候出现如下错误: 错误描述为:在main函数中,进行函数max()重载时,出现(ambiguous)含糊的,不明确的 ...
- max 宏定义取消:error C2589: error C2059: 语法错误 : “::”
原文链接:http://blog.csdn.net/danelumax2/article/details/9172465有修改! 一:关于Pcl和WIndef的冲突: 1. 错误输出 ./zlibra ...
- 高德地图引入库错误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 ...
- PCL -语法错误:“::” error C2589: “(”:“::”右边的非法标记
1.错误原因:系统函数与pcl中的max函数冲突导致的 2.两种解决办法: 1)错误中max和min函数用括号括起来,例如"std::Max"修改为“(std::Max)”. 2) ...
随机推荐
- C#全局钩子和局部钩子记录
源自:https://blog.csdn.net/programvae/article/details/80292076 最近碰巧要使用键盘钩子,于是在网上搜索了一番,发现大多数博客的文章都是雷同的, ...
- RTMP、RTSP、HTTP视频协议详解(附:直播流地址、播放软件)
- python自动化之图像
''' RGBA值:指定颜色中的红.绿.蓝和alpha(透明度)的值 RGBA 名称 (255,255,255,2 ...
- 对HashMap的理解(一):HashMap的实现
一.HashMap介绍 1. 定义HashMap实现了Map接口,继承AbstractMap类.其中Map接口定义了键映射到值的规则,而AbstractMap类提供 Map 接口的骨干实现,以最大限度 ...
- NOIP赛前集训营-提高组(第一场)#B 数数字
题目描述 小N对于数字的大小一直都有两种看法.第一种看法是,使用字典序的大小(也就是我们常用的判断数字大小的方法,假如比较的数字长度不同,则在较短一个前面补齐前导0,再比较字典序),比如43<3 ...
- BZOJ3836 [Poi2014]Tourism 【树形dp +状压dp】
题目链接 BZOJ3836 题解 显然这是个\(NP\)完全问题,此题的解决全仗任意两点间不存在节点数超过10的简单路径的性质 这意味着什么呢? \(dfs\)树深度不超过\(10\) \(10\)很 ...
- Luogu P3251 [JLOI2012]时间流逝 期望dp
题面 题面 题解 期望\(dp\)好题! 今年\(ZJOI\)有讲过这题... 首先因为\(T\)只有\(50\),大力\(dfs\)后发现,可能的状态数最多只有\(20w\)左右,所以我们就可以大力 ...
- Zabbix利用msmtp+mutt发送邮件报警
操作系统:CentOS 7 Web环境:Nginx+MySQL+PHP zabbix版本:zabbix-2.4.8.tar.gz 邮件服务:msmtp-1.4.32.tar.bz2 #http ...
- 我们使用git checkout 将B分支上的部分页面代码 添加或覆盖到A分支上
$ git branch * A B $ git checkout B message.html message.css message.js other.js $ git status # On b ...
- Html和websocket初识
一.web框架 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. import socket def handle_request(c ...