C++ 如何处理 windows.h 中的 max 宏与 std 中的 max 冲突?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11544073/
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-27 15:14:08  来源:igfitidea点击:

How do I deal with the max macro in windows.h colliding with max in std?

c++iostreamcin

提问by Almo

So I was trying to get valid integer input from cin, and used an answer to this question.

所以我试图从 cin 获取有效的整数输入,并使用了这个问题的答案。

It recommended:

它建议:

#include <Windows.h> // includes WinDef.h which defines min() max()
#include <iostream>
using std::cin;
using std::cout;

void Foo()
{
    int delay = 0;
    do
    {
        if(cin.fail())
        {
            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
        cout << "Enter number of seconds between submissions: ";
    } while(!(cin >> delay) || delay == 0);
}

Which gives me an error on Windows, saying that the maxmacro doesn't take that many arguments. Which means I have to do this

这在 Windows 上给了我一个错误,说max宏不接受那么多参数。这意味着我必须这样做

do
{
    if(cin.fail())
    {
        cin.clear();
#undef max
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    cout << "Enter number of seconds between submissions: ";
} while(!(cin >> delay) || delay == 0);

To get it to work. That's pretty ugly; is there a better way to work around this issue? Maybe I should be storing the definition of maxand redefining it afterward?

让它工作。这很丑陋;有没有更好的方法来解决这个问题?也许我应该存储定义max并在之后重新定义它?

回答by hmjd

Define the macro NOMINMAX:

定义宏NOMINMAX

This will suppress the min and max definitions in Windef.h.

这将抑制 Windef.h 中的 min 和 max 定义。

回答by NIRO

Just wrap the function name in parenthesis:

只需将函数名称括在括号中:

(std::numeric_limits<size_type>::max)()

No need for the NOMINMAX macro in this case, plus you won't get compiler warnings

在这种情况下不需要 NOMINMAX 宏,而且您不会收到编译器警告

回答by Littlegator

Are you just trying to flush the cin buffer? I always just used:

你只是想刷新cin缓冲区吗?我总是只使用:

cin.ignore(cin.rdbuf()->in_avail());

回答by Philipp

If you don't know whether somebody else might have included windows.hwithout NOMINMAX, you might define a dummy macro which can be used to suppress function-like macro invocations without changing the definition:

如果您不知道其他人是否可能包含windows.hwithout NOMINMAX,您可以定义一个虚拟宏,该宏可用于在不更改定义的情况下抑制类似函数的宏调用:

#define DUMMY
...
std::numeric_limits<std::streamsize>::max DUMMY ()

Not really pretty either, but works and is non-intrusive.

也不是很漂亮,但有效且非侵入性。

When working with the Windows header file, I prefer to hide it as much as I can by including it only in specialized code and header files (using pimpl if necessary), because it throws just too much garbage into the global namespace.

在使用 Windows 头文件时,我更喜欢通过仅将其包含在专门的代码和头文件中(如有必要,使用 pimpl)来尽可能地隐藏它,因为它向全局命名空间中抛出了太多垃圾。

回答by AntonK

If you happen to use GDI+, the approach with NOMINMAXwon't work for you, because headers of GDI+ require minor maxin global namespace.

如果您碰巧使用 GDI+,则该方法NOMINMAX对您不起作用,因为 GDI+ 的标头需要minmax在全局命名空间中。

And the simplest workaround in this case is to undefine min/maxwhen they are no longer needed.

在这种情况下,最简单的解决方法是在不再需要时取消定义min/ max

The code sample to illustrate the approach:

用于说明该方法的代码示例:

//#define NOMINMAX - this won't work
#include <Windows.h>
#include <gdiplus.h>
#undef max
#undef min
...
#include <cxxopts.hpp>