I'm Michael Suodenjoki - a software engineer living in Kgs. Lyngby, north of Copenhagen, Denmark. This is my personal site containing my blog, photos, articles and main interests.
I'm Michael Suodenjoki - a software engineer living in Kgs. Lyngby, north of Copenhagen, Denmark. This is my personal site containing my blog, photos, articles and main interests.
Updated 2013.11.26 22:24 +0100 |
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.
The solution is one or several of the following (prioritized with best approach first):
#include <algorithm>
using namespace std;
// use min() instead of std::min()
// use max() instead of std::max()
#include <algorithm>
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#include <gdiplus.h>
#undef max
#undef min
using namespace std;
#ifndef max
#define max __max
#endif
#ifndef min
#define min __min
#endif
More (updated November 26th 2013):