max(a,b) 是否在 stdlib.h 中定义?
我使用两台计算机,每台计算机都有不同版本的 Visual Studio。在 Visual Studio 2008 计算机上编译我的代码。在 Visual 2010 计算机上,我的代码无法编译,因为我使用了宏 max(a,b)
,据我所知,该宏是在 stdlib.h 中定义的。我不能只定义 max(a,b)
因为它将在 Visual 2008 计算机上重新定义。但是,如果我不定义 max(a,b)
,我的代码将无法在 Visual 2010 计算机上编译。
有什么解决办法吗?
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.
Any solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
任何在其标准头文件中定义名为
max
的宏的 C 库都超出了想象。幸运的是,如果您需要支持此类平台,一个简单的解决方法是在包含系统标头之后和您自己的任何标头/代码之前使用#undef max
(以及它定义的任何其他有问题的宏)。请注意,其他人都说将您的定义包装在
#ifndef max ... #endif
中。这不是一个好主意。在系统标头中定义max
表明实现者不称职,并且某些版本的环境可能具有不正确宏(例如,不正确的宏)用括号保护参数,但我一生中至少见过一次max
宏错误地执行min
而不是max
! )。只要使用#undef
就可以保证安全。至于为什么
stdlib.h
定义max
如此糟糕,C 标准对于为应用程序保留哪些名称以及为标准函数保留哪些名称非常具体和/或实现的内部使用。这是有充分理由的。在系统头文件中定义可能与应用程序中使用的变量/函数名称冲突的宏名称是危险的。在最好的情况下,它会导致编译时错误,原因很明显,但在其他情况下,它可能会导致非常奇怪的行为,难以调试。无论如何,编写可移植代码都变得非常困难,因为您永远不知道库已经采用了哪些名称。Any C library which defines a macro named
max
in 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.Note that everyone else is saying to wrap your definition in
#ifndef max ... #endif
. This is not a good idea. Definingmax
in a system header is an indication that the implementor was incompetent, and it's possible that certain versions of the environment have incorrect macros (for example, ones which do not properly protect arguments with parentheses, but I've even seen amax
macro that was incorrectly performingmin
instead ofmax
at least once in my life!). Just use#undef
and be safe.As for why it's so broken for
stdlib.h
to definemax
, 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.所以回答你的主要问题:
不,不是,它是在 Windef.h 的第 187 行左右定义的:
So answering your main question:
No it isn't, it's defined in windef.h around line 187:
使用
#ifndef
保护它。请记住,上面的版本不如内联函数安全,例如 max(a++,b--) 会导致意外的结果。
Protect it with an
#ifndef
.Keep in mind that the version above is not as safe as an inline function, e.g.
max(a++,b--)
will cause unxpected results.您可以使用条件编译:
you can use condition compiling:
在 Visual C++ 中,如果在包含标准头之前
#define NOMINMAX
,则不会获得宏max
或min
。In Visual C++, if you
#define NOMINMAX
before including the standard headers, you will not get a macromax
ormin
.