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 错误,码迷,mamicode.com

时间: 2024-11-10 07:52:32

std::max 错误的相关文章

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

[ c++] cmake 编译时 undefined reference to `std::cout&#39; 错误的解决方案

cmake ..  和 make 之后,出现如下错误 Linking CXX executable ../../../bin/ModuleTest CMakeFiles/ModuleTest.dir/tmp.cpp.o: In function `main': /ModuleTest/tmp.cpp:4: undefined reference to `std::cout' tmp.cpp 内容如下 1 #include <iostream> 2 3 int main(){ 4 std::co

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

代码 #include<iostream>#include <algorithm> // std::min#undef minint main(){ float a =15.0f; float c ; c=std::min(10,a); printf("%f",b); getchar();} 错误提示 Error 1 error C2780: 'const _Ty &std::min(const _Ty &,const _Ty &,_Pr

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

错误描述: 今天在使用Qt写一个C++函数模板的测试程序的时候,编译的时候,编译的时候出现如下错误: 错误描述为:在main函数中,进行函数max()重载时,出现(ambiguous)含糊的,不明确的;引起歧义的使用; 因为第一次遇到这种错误,写篇内容纪念一下吧. 测试代码如下: #include <iostream> using namespace std; template <typename T>//typename == class T max(T a,T b) { ret

第十六周项目3:max带来的冲突

问题及代码: /* *Copyright (c)2015,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:project.cpp *作 者:陈文青 *完成日期:2015年6月28日 *版 本 号:v1.0 * *问题描述:分析下面程序出现的编译错误,给出解决的方案. *程序输入: *程序输出: */ #include<iostream> using namespace std; //定义函数模板 template<class T> T max(

第十六周 项目三-max带来的冲突

分析下面程序出现的编译错误,给出解决的方案. #include<iostream> using namespace std; //定义函数模板 template<class T> T max(T a, T b) { return (a>b)?a:b; } int main() { int x=2,y=6; double x1=9.123,y1=12.6543; cout<<"把T实例化为int:"<<max(x,y)<<

第16周上机实践项目3——max带来的冲突

分析下面程序出现的编译错误,给出解决的方案. #include<iostream> using namespace std; //定义函数模板 template<class T> T max(T a, T b) { return (a>b)?a:b; } int main() { int x=2,y=6; double x1=9.123,y1=12.6543; cout<<"把T实例化为int:"<<max(x,y)<<

error C2589: &ldquo;(&rdquo;: &ldquo;::&rdquo;右边的非法标记 error C2059: 语法错误 : &ldquo;::

1. 错误输出 ./zlibrary/ui/src/win32/w32widgets/W32VBorderBox.cpp(114) : error C2589: "(": "::"右边的非法标记    ./zlibrary/ui/src/win32/w32widgets/W32VBorderBox.cpp(114) : error C2059: 语法错误 : "::"2. 错误代码举例 size.Width = std::max(size.Wid

STL algorithm算法max,max_elements(33)

max原型: std::max C++98 C++11 C++14 default (1) template <class T> const T& max (const T& a, const T& b); custom (2) template <class T, class Compare> const T& max (const T& a, const T& b, Compare comp); initializer list