Visual C++ 上 NOMINMAX 的可能问题

2023-06-03C/C++开发问题
2

本文介绍了Visual C++ 上 NOMINMAX 的可能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在我的程序中的任何其他内容之前定义 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.

这篇关于Visual C++ 上 NOMINMAX 的可能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6