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) ...
随机推荐
- H5实现的时钟
源码如下: <!doctype html> <html> <head></head> <body> <canvas id=" ...
- Java多线程之ThreadLocal总结2
ThreadLocal是什么 早在JDK 1.2的版本中就提供Java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁地 ...
- delphi adoquery的post和UpdateBatch
delphi adoquery的post和UpdateBatch Post是确认当前的修改,而UpdateBatch是把已经确认但是没有存盘的数据写入数据库当ADOQuery的CursorType是c ...
- CSS实现点击改变元素背景色
可通过使用css伪类实现点击元素变色的效果,两个伪类是:active, :focus :active :active选择器用于选择活动链接.当在一个链接上点击时,它就会成为活动的(激活的),:acti ...
- 第220天:Angular---路由
内容介绍,为什么要使用前端路由? 在2005左右,兴起了一种叫做ajax的技术,有了ajax之后,我们向服务端提交数据的时候就不再需要使用from表单去提交了,因为from表单之间的提交会导致页面之间 ...
- 题解 CF762A 【k-th divisor】
emmm...不能说是水题吧...小金羊以为考的是STL(手动滑稽)... 行,这个题说让分解因数(不一定非得质因数). 我们考虑到有第k个数有可能有\(x\cdot x=n\)的毒瘤情况, 并且题目 ...
- Netty基础系列(3) --彻底理解NIO
前言 上一节中我们提到了同步异步与阻塞非阻塞的区别,知道了同步并不等于阻塞.而本节的主角NIO是一种同步非阻塞的I/O模型,并且是I/O多路复用模型.NIO在java中被称为 New I/O.它并不能 ...
- 【原创】 PLSQL 索引排序优化
转载请注明出处 select /*+INDEX_DESC(T IDX_SYS_MESS_CREATE_DATE)*/ * FROM sys_message T where t.create_date ...
- dp的一些计划
抱歉这是鸽子贴. 树形dp [x][[POI2014]HOT-Hotels](https://www.luogu.org/problemnew/show/P3565) [x][[HAOI2015]树上 ...
- 【BZOJ1054】移动玩具(搜索)
[BZOJ1054]移动玩具(搜索) 题面 BZOJ 洛谷 题解 这种小清新搜索题写出来好舒服啊. 要是原来的我来写代码肯定又臭又长吧.. #include<cstdio> #includ ...