C++ 为什么包含 windows.h 时 std::min 失败?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5004858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:08:38  来源:igfitidea点击:

Why is std::min failing when windows.h is included?

c++visual-studio-2005

提问by hidayat

#include <algorithm>
#include <Windows.h>

int main()
{
    int k = std::min(3, 4);
    return 0;
}

What is windows doing if I include Windows.h? I can't use std::minin visual studio 2005. The error message is:

如果我包含 Windows.h,Windows 会做什么?我无法std::min在 Visual Studio 2005 中使用。错误消息是:

error C2589: '(' : illegal token on right side of '::'
error C2059: syntax error : '::'

回答by paxdiablo

The windows.hheader file (or more correctly, windef.hthat it includes in turn) has macros for minand maxwhich are interfering.

windows.h头文件(或更正确地说windef.h,它包括反过来)具有用于宏minmax被干扰。

You should #define NOMINMAXbefore including it.

你应该#define NOMINMAX在包括它之前。

回答by PolyMesh

No need to define anything, just bypass the macro using this syntax:

无需定义任何东西,只需使用以下语法绕过宏:

(std::min)(a, b); // added parentheses around function name
(std::max)(a, b);

回答by Gene Bushuyev

As others mentioned, the errors are due to min/max macros that are defined in windows header(s). There are three ways of disabling them.

正如其他人所提到的,这些错误是由于 Windows 标题中定义的最小/最大宏造成的。有三种方法可以禁用它们。

1) #define NOMINMAXbefore including header, this is generally a bad technique of defining macros in order to affect the following headers;

1)#define NOMINMAX在包含头文件之前,这通常是定义宏以影响以下头文件的糟糕技术;

2) define NOMINMAXin compiler command line/IDE. The bad part about this decision is that if you want to ship your sources, you need to warn the users to do the same;

2)NOMINMAX在编译器命令行/IDE 中定义。这个决定的坏处是,如果你想发布你的源代码,你需要警告用户也这样做;

3) simply undefine the macros in your code before they are used

3)在使用之前简单地取消定义代码中的宏

#undef min
#undef max

This is probably the most portable and flexible solution.

这可能是最便携和最灵活的解决方案。

回答by Erik

I still have trouble occasionally with the windows headers and project wide define of NOMINMAX doesn't always seem to work. As an alternative to using parentheses, I sometimes make the type explicit like so:

我偶尔仍然遇到 Windows 标题的问题,并且 NOMINMAX 的项目范围定义似乎并不总是有效。作为使用括号的替代方法,我有时会像这样使类型显式:

int k = std::min<int>(3, 4);

This also stops the preprocessor from matching to minand is arguably more readable than the parentheses workaround.

这也会阻止预处理器min与括号匹配,并且可以说比括号解决方法更具可读性。

回答by Jerry Coffin

Try something like this:

尝试这样的事情:

#define NOMINMAX
#include <windows.h>

By default, windows.h defines minand maxas macros. When those are expanded, code that tries to use std::min(for example) will end up looking something like this:

默认情况下,windows.h 将min和定义max为宏。当这些被展开时,尝试使用的代码std::min(例如)最终看起来像这样:

int k = std::(x) < (y) ? (x) : (y);

The error message is telling you that std::(x)isn't allowed.

错误消息告诉您这std::(x)是不允许的。

回答by Terry

In my case, project did not include windows.hor windef.hexplicitly. It was using Boost. So, I resolved the issue by going to the project Properties -> C/C++ -> Preprocessor, and appending NOMINMAXin the Preprocessor Definitions(VS 2013, VS 2015).

就我而言,项目没有包括windows.hwindef.h明确。它正在使用Boost。所以,我首先要对项目解决了这一问题Properties -> C/C++ -> Preprocessor,并附加NOMINMAXPreprocessor Definitions(VS 2013,2015年VS)。

回答by Foo Bah

#define NOMINMAX

is the trick to suppress the macro definitions of max and min

是抑制 max 和 min 的宏定义的技巧

http://support.microsoft.com/kb/143208

http://support.microsoft.com/kb/143208

回答by Foo Bah

For people including windows.h, put the following in effected headers:

对于包括 windows.h 在内的人,请将以下内容放入受影响的标题中:

#include windows headers ...

pragma push_macro("min")
pragma push_macro("max")
#undef min
#undef max

#include headers expecting std::min/std::max ...

...

pragma pop_macro("min")
pragma pop_macro("max")

In source files just #undef min and max.

在源文件中只有 #undef min 和 max。

#include windows headers ...

#undef min
#undef max

#include headers expecting std::min/std::max ...

回答by Inline

To solve this issue I just create header file named fix_minmax.hwithoutinclude guards

为了解决这个问题,我只是创建了一个fix_minmax.h没有包含保护的头文件

#ifdef max
    #undef max
#endif

#ifdef min
    #undef min
#endif

#ifdef MAX
    #undef MAX
#endif
#define MAX max

#ifdef MIN
   #undef MIN
#endif
#define MIN min

#include <algorithm>
using std::max;
using std::min;

Basic usage is like this.

基本用法是这样的。

// Annoying third party header with min/max macros
#include "microsoft-mega-api.h"
#include "fix_minmax.h"

Pros of this approach is that it works with every kind of included file or part of code. This also saves your time when dealing with code or libraries that depend on min/maxmacros

这种方法的优点是它适用于各种包含的文件或部分代码。这也可以节省您处理依赖min/max宏的代码或库的时间

回答by sstn

I'd assume windows.h does define min as a macro, e.g. like

我假设 windows.h 确实将 min 定义为宏,例如

#define min(a,b)  ((a < b) ? a : b)

That would explain the error message.

这将解释错误消息。