C语言 max(a,b) 是否在 stdlib.h 中定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4234004/
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
Is max(a,b) defined in stdlib.h or not?
提问by snakile
I'm using two computers, each with a different version of visual studio. On the visual studio 2008 computer my code compiles. On the visual 2010 computer my code doesn't compile because I'm using the macro max(a,b)which as far as I know is defined in stdlib.h. I cannot just define max(a,b)because it'll be a redefinition on the visual 2008 computer. But if I don't define max(a,b)my code doesn't compile on the visual 2010 computer.
我正在使用两台计算机,每台计算机都有不同版本的 Visual Studio。在 Visual Studio 2008 计算机上我的代码编译。在 Visual 2010 计算机上,我的代码无法编译,因为我使用的max(a,b)是在 stdlib.h 中定义的宏。我不能仅仅定义,max(a,b)因为它会在visual 2008 计算机上重新定义。但是如果我不定义max(a,b)我的代码就不能在visual 2010 计算机上编译。
Any solution?
有什么解决办法吗?
回答by R.. GitHub STOP HELPING ICE
Any C library which defines a macro named maxin its standard headers is broken beyond imagination. Fortunately, an easy workaround if you need to support such platforms is to #undef max(and any other problematic macros it defines) after including the system headers and before any of your own headers/code.
任何定义max在其标准头文件中命名的宏的 C 库都超出了想象。幸运的是,如果您需要支持此类平台,一个简单的解决方法是#undef max(以及它定义的任何其他有问题的宏)在包含系统头文件之后和您自己的任何头文件/代码之前。
Note that everyone else is saying to wrap your definition in #ifndef max ... #endif. This is nota good idea. Defining maxin a system header is an indication that the implementor was incompetent, and it's possible that certain versions of the environment have incorrectmacros (for example, ones which do not properly protect arguments with parentheses, but I've even seen a maxmacro that was incorrectly performing mininstead of maxat least once in my life!). Just use #undefand be safe.
请注意,其他人都在说要将您的定义包装在#ifndef max ... #endif. 这不是一个好主意。max在系统头文件中定义表明实现者不称职,并且某些版本的环境可能有不正确的宏(例如,那些没有正确保护带括号的参数,但我什至看到一个max宏是错误地执行min而不是max我一生中至少一次!)。只需使用#undef并确保安全。
As for why it's so broken for stdlib.hto define max, the C standard is very specific about what names are reserved for the application and what names are reserved for standard functions and/or internal use by the implementation. There are very good reasons for this. Defining macro names in system headers that could clash with variable/function names used in the application program is dangerous. In the best case it leads to compile-time errors with an obvious cause, but in other cases it can cause very strange behavior that's hard to debug. In any case it makes it very difficult to write portable code because you never know what names will already be taken by the library.
至于为什么stdlib.h定义如此糟糕max,C 标准对于为应用程序保留哪些名称以及为标准函数和/或实现内部使用保留哪些名称非常具体。这有很好的理由。在可能与应用程序中使用的变量/函数名称冲突的系统头文件中定义宏名称是危险的。在最好的情况下,它会导致明显原因的编译时错误,但在其他情况下,它会导致非常奇怪的行为,很难调试。在任何情况下,编写可移植代码都非常困难,因为您永远不知道库将采用哪些名称。
回答by Calmarius
So answering your main question:
所以回答你的主要问题:
Is max(a,b) defined in stdlib.h or not?
max(a,b) 是否在 stdlib.h 中定义?
No it isn't, it's defined in windef.h around line 187:
不,不是,它是在windef.h 中第187 行左右定义的:
#ifndef NOMINMAX #ifndef max #define max(a,b) (((a) > (b)) ? (a) : (b)) #endif #ifndef min #define min(a,b) (((a) < (b)) ? (a) : (b)) #endif #endif /* NOMINMAX */
#ifndef NOMINMAX #ifndef max #define max(a,b) (((a) > (b)) ? (a) : (b)) #endif #ifndef min #define min(a,b) (((a) < (b)) ? (a) : (b)) #endif #endif /* NOMINMAX */
回答by John Ledbetter
Protect it with an #ifndef.
用#ifndef.
#ifndef max
#define max(a,b) ((a) > (b) ? (a) : (b))
#endif
Keep in mind that the version above is not as safe as an inline function, e.g. max(a++,b--)will cause unxpected results.
请记住,上面的版本不如内联函数安全,例如max(a++,b--)会导致意外结果。
回答by Vladimir Ivanov
you can use condition compiling:
您可以使用条件编译:
#ifndef max
#define max(a,b) ...
#endif
回答by Bill Lynch
In Visual C++, if you #define NOMINMAXbefore including the standard headers, you will not get a macro maxor min.
在 Visual C++ 中,如果您#define NOMINMAX在包含标准头文件之前,您将不会获得宏max或min.

