C++ #define NOMINMAX 使用 std::min/max
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13416418/
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
#define NOMINMAX using std::min/max
提问by NaturalDemon
i recently added:
我最近补充说:
#define NOMINMAX
#include <Windows.h>
#include <algorithm>
to my main.cpp in order to use
到我的 main.cpp 以便使用
std::max( x , x ); // x is just a placeholder and not actual anything
std::min( x , x );
but i can't use std::max()/std::min()
in other files.
但我不能std::max()/std::min()
在其他文件中使用。
error C2589: '(' : illegal token on right side of '::'
error C2059: syntax error : '::'
i tried to add #define NOMINMAX
in my other files, but fails. what is the clue?
我试图添加#define NOMINMAX
我的其他文件,但失败了。线索是什么?
i looked around before asking, but i don't understand the answer Possible problems with NOMINMAX on Visual C++
我在问之前环顾四周,但我不明白答案可能存在 NOMINMAX 在 Visual C++ 上的问题
回答by Pete Becker
If you're really desperate, put parentheses around the function names:
如果您真的很绝望,请在函数名称周围加上括号:
(std::min)(x, y);
This syntax won't apply a function-like macro. (Formally, to apply a function-like macro the name of the macro must be followed by optional white space then a '('.)
此语法不会应用类似函数的宏。(正式地,要应用类似函数的宏,宏的名称必须后跟可选的空格,然后是“(”。)
回答by hmjd
Define NOMINMAX
via a compiler flag:
NOMINMAX
通过编译器标志定义:
> cl.exe -DNOMINMAX ...
this will then be defined for all of the source files. I don't use the IDEs but this page provides guidance on navigating the IDE to set this: Using STL in Windows Program Can Cause Min/Max Conflicts :
这将为所有源文件定义。我不使用 IDE,但此页面提供了有关导航 IDE 以进行设置的指导:在 Windows 程序中使用 STL 会导致最小/最大冲突 :
Simply define the NOMINMAX preprocessor symbol. This can be done in the Developer Studio project under Build, Settings, on the C/C++ tab, in the Preprocessor category. This will suppress the min and max definitions in Windef.h.
只需定义 NOMINMAX 预处理器符号。这可以在 Developer Studio 项目中的 Build、Settings 下的 C/C++ 选项卡上的 Preprocessor 类别中完成。这将抑制 Windef.h 中的 min 和 max 定义。
回答by David Gausmann
If you define NOMINMAX, because you prefer the STL version, then you may get problems while including gdiplus.h, which uses the min/max macro. As solution you need to include the STL headers and use "using namespace std" beforeyou include the gdiplus.h.
如果您定义 NOMINMAX,因为您更喜欢 STL 版本,那么在包含使用 min/max 宏的 gdiplus.h 时可能会遇到问题。作为解决方案,您需要在包含gdiplus.h之前包含STL 标头并使用“使用命名空间 std” 。
In example:
例如:
#define NOMINMAX
// Include C++ headers
#include <algorithm>
using namespace std;
// Include Windows headers
#include <windows.h>
#include <gdiplus.h>
回答by Tom
It's likely that your problem is that you #define NOMINMAX
after you #include "windows.h"
. It is important that the #define
come first.
你的问题很可能是你#define NOMINMAX
在追求你#include "windows.h"
。重要的#define
是先来。
The reason is that windows.h (actually I think windef.h, which is included by windows.h) has code similar to this:
原因是windows.h(其实我觉得windef.h,是windows.h中包含的)有类似这样的代码:
#ifndef NOMINMAX
#define min(x,y) ((x) < (y) ? (x) : (y))
#define max(x,y) ((x) > (y) ? (x) : (y))
#endif
So #define NOMINMAX
is telling the compiler (or actually the preprocessor) to skip over the definitions of min
and max
, but it will only apply if you do it before you #include "windows.h"
.
所以#define NOMINMAX
告诉编译器(或实际预处理器)跳过的定义min
和max
,但如果你在你面前做只会适用#include "windows.h"
。