C++ min max Macro When Including Windows API

This is yet another quirks of C++ programming I found (and could be very hard to debug).

When you use Windows API (eg: you have #include <afxwin.h>), it will pull min() and max() macro.

Apparently this will cause confusion for the compiler if you’re including another 3rd party library which expects standard library min() and max(). The error message you get is something like this (how obscure is this?):

1>c:\mongodbsrc\src\mongo\db\../bson/bsonelement.h(630): warning C4003: not enough actual parameters for macro 'max'
1>c:\mongodbsrc\src\mongo\db\../bson/bsonelement.h(630): error C2589: '(' : illegal token on right side of '::'
1>c:\mongodbsrc\src\mongo\db\../bson/bsonelement.h(630): error C2059: syntax error : '::'
1>c:\mongodbsrc\src\mongo\db\../bson/bsonelement.h(630): error C2143: syntax error : missing ';' before '{'

To avoid pulling the macros from Windows API, hash define following:

#define NOMINMAX

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s