Visual C++ 上 NOMINMAX 的可能问题

2021-12-22 00:00:00 windows visual-c++ c++

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

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

据我所知,这将使 没有定义 minmax 宏,从而导致许多冲突使用 STL,例如std::min()std::max()std::numeric_limits::min() 被解析.

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

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

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 标头会出现问题吗?

Will there be be problems with other Windows headers?

推荐答案

使用 NOMINMAX 是包含 的唯一不完全邪恶的方式.您还应该定义 UNICODESTRICT.尽管后者是现代实现默认定义的.

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

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

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.

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

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):

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

请注意,这与 using namespace std; 在标题中的全局范围内有很大不同,后者永远不要.

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

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

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.

相关文章