Visual C++ 上 NOMINMAX 的可能问题

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

Possible problems with NOMINMAX on Visual C++

c++windowsvisual-c++

提问by Manuel

What problems could I get when defining NOMINMAXbefore anything else in my program?

NOMINMAX在我的程序中的其他任何内容之前进行定义时,我会遇到什么问题?

As far as I know, this will make <Windows.h>not define the minand maxmacros such that many conflicts with the STL, e.g. std::min(), std::max(), or std::numeric_limits<T>::min()are resolved.

据我所知,这将<Windows.h>不会定义minmax宏,从而解决许多与 STL 的冲突,例如std::min()std::max()、 或std::numeric_limits<T>::min()

Am I right in the assumption that only Windows-specific and legacy code will have problems? Almost all libraries should not depend on min()and max()defined as macros?

我是否正确地假设只有 Windows 特定和遗留代码会出现问题?几乎所有的库都不应该依赖min()max()定义为宏?

Edit:Will there be be problems with other Windows headers?

编辑:其他 Windows 标头会出现问题吗?

回答by Cheers and hth. - Alf

Using NOMINMAXis the only not-completely-evil way to include <windows.h>. You should also define UNICODEand STRICT. Although the latter is defined by default by modern implementations.

使用NOMINMAX是包含<windows.h>. 您还应该定义UNICODESTRICT。尽管后者是由现代实现默认定义的。

You can however run into problems with Microsoft’s headers, e.g. for GdiPlus. I’m not aware of problems with headers from any other companies or persons.

但是,您可能会遇到 Microsoft 标头的问题,例如 GdiPlus。我不知道任何其他公司或个人的标题有问题。

If the header defines a namespace, as GdiPlus does, then one fix is to create a wrapper for the relevant header, where you include <algorithm>, and inside the header’s namespace, using namespace std;(or alternatively using std::min;and using std::max):

如果标头定义了一个命名空间,就像 GdiPlus 所做的那样,那么一种解决方法是为相关标头创建一个包装器,在其中包含<algorithm>, 并在标头的命名空间内,using namespace std;(或者using std::min;using std::max):

#define NOMINMAX
#include <algorithm>
namespace Gdiplus
{
  using std::min;
  using std::max;
}

Note that that is very different from a using namespace std;at global scope in header, which one should never do.

请注意,这using namespace std;与标头中的at 全局范围非常不同,这是永远不应该做的

I don’t know of any good workaround for the case where there's no namespace, but happily I haven’t run into that, so in practice that particular problem is probably moot.

对于没有命名空间的情况,我不知道有什么好的解决方法,但很高兴我没有遇到这种情况,所以实际上这个特定问题可能没有实际意义。

回答by Adrian McCarthy

I generally use NOMINMAXlike this to limit the potential side effects:

我通常这样使用NOMINMAX来限制潜在的副作用:

#define NOMINMAX
#include <windows.h>
#undef NOMINMAX

That way the scope of the NOMINMAXis relatively confined.

这样一来,范围NOMINMAX就相对有限了。

It's not a perfect solution. If something else has already defined NOMINMAX, this pattern fails (though I've never encountered such a case).

这不是一个完美的解决方案。如果已经定义了其他内容NOMINMAX,则此模式失败(尽管我从未遇到过这种情况)。

If you want to be really, really careful, then you can #include a wrapper header wherever you would have #included windows.h. The wrapper would go something like this:

如果你真的非常非常小心,那么你可以在任何你有 #included windows.h 的地方 #include 一个包装头。包装器会是这样的:

/* Include this file instead of including <windows.h> directly. */
#ifdef NOMINMAX
#include <windows.h>
#else
#define NOMINMAX
#include <windows.h>
#undef NOMINMAX
#endif

You could imagine doing other things in the wrapper, too, like enforcing UNICODEand/or STRICT.

您也可以想象在包装器中做其他事情,例如强制UNICODE和/或STRICT.

回答by vladon

For precompiled header (like stdafx.h) I use this:

对于预编译头文件(如 stdafx.h),我使用这个:

#define NOMINMAX
#include <algorithm>
#include <Windows.h>
#ifndef min
#define min(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef max
#define max(x,y) ((x) > (y) ? (x) : (y))
#endif
#include <gdiplus.h>
#undef min
#undef max

回答by Anil8753

I got fix issue by declaring the headers and namespaces in the following order:

我通过按以下顺序声明标头和命名空间来解决问题:

#include <windows.h>
#include <minmax.h>
#include <gdiplus.h>

using namespace Gdiplus;
using namespace std;