The min/max problem in C++ and Windows

本文探讨了Windows头文件中定义的min/max宏可能与C++标准库中的std::min()/std::max()函数引发的冲突及错误问题,并提供了多种解决方案以避免此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cite from http://www.suodenjoki.dk/us/archive/2010/min-max.htm

I recently discovered that a Windows header file - specifically the WinDef.h - defines two macros min and max which may result in conflicts and compiler errors.

You'll hit compiler errors such as:

error C2589: '(' : illegal token on right side of '::'

Any C++ source code including the Windows header will likely head into problems if min or max are used together with the Standard C++ library functions std::min()/std::max() as defined in <algorithm>.

The issue is that using a macro definition for such common names is a bad idea and the min/max problem itself has been there for several years (a Google search can confirm that). So the Windows header should never had defined these macros.

Note: It beats me why Microsoft haven't changed WinDef.h years ago. Also as the min/max are macros they should have been more clearly marked as such - using the common syntactic practice of using MIN/MAX in uppercase.

Note: You should note that Microsoft also have the __min()/__max() C functions as declared in Visual C++'s <stdlib.h> (for C old-stylers) or <cstdlib> (For C++ coolers). These can be used as alternatives, but you should really prefer using std::min() and std::max() because these are part of the C++ standard. Remember that Microsoft prefixes non standard structures and functions with underscore '_' character.

Solution

The solution is one or several of the following (prioritized with best approach first):

  1. Convert your source code to use the Standard C++ std::min() and std::max() and ensure that you #include <algorithm>. Use
    1 #include <algorithm>
    2 using namespace std;
    3  
    4 // use min() instead of std::min()
    5 // use max() instead of std::max()
  2. Define the NOMINMAX macro to instruct WinDef.h not to define the min/max macros. E.g. you can update your Visual C++ compiler options with /D NOMINMAX or you can insert #define NOMINMAX.
  3. Redefine min/max in the specific problematic files. Especially this may be needed in you include gdiplus Windows headers files, because these uses the Windows min/max macros.
    01 #include <algorithm>
    02  
    03 #ifndef max
    04 #define max(a,b) (((a) > (b)) ? (a) : (b))
    05 #endif
    06 #ifndef min
    07 #define min(a,b) (((a) < (b)) ? (a) : (b))
    08 #endif
    09  
    10 #include <gdiplus.h>
    11 #undef max
    12 #undef min
    13  
    14 using namespace std;
  4. Alternatively redefine min/max to use use the Visual C++ non-standard __min()/__max(). But min/max are still defined as macros and will likely lead to problems. Not a good solution.
    1 #ifndef max
    2 #define max __max
    3 #endif
    4 #ifndef min
    5 #define min __min
    6 #endif
  5. Alternatively use Visual C++ compiler options /Dmin=__min / Dmax=__max. This will tell compiler and WinDef.h not to define min/max macros and use __min() and __max() functions instead as defined in <cstdlib> (or <stdlib.h>) so ensure this is included. But min/max are still defined as macros and will likely lead to problems. Not a good solution.

More (updated November 26th 2013):

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值