Manuel Manuel - 5 months ago 35
C++ Question

Possible problems with NOMINMAX on Visual C++

What problems could I get when defining

before anything else in my program?

As far as I know, this will make
not define the
macros such that many conflicts with the STL, e.g.
, or
are resolved.

Am I right in the assumption that only Windows-specific and legacy code will have problems?
Almost all libraries should not depend on
defined as macros?

Edit: Will there be be problems with other Windows headers?


Using NOMINMAX is the only not-completely-evil way to include <windows.h>. You should also define UNICODE and STRICT. Although the latter is defined by default by modern implementations.

You can however run into problems with Microsoft’s headers, e.g. for GdiPlus. I’m not aware of problems with headers from any other companies or persons.

If the header defines a namespace, as GdiPlus does, then one fix is to create a wrapper for the relevant header, where you include <algorithm>, and inside the header’s namespace, using namespace std; (or alternatively using std::min; and using std::max):

#define NOMINMAX
#include <algorithm>
namespace Gdiplus
  using std::min;
  using std::max;

Note that that is very different from a using namespace std; at global scope in header, which should never do.

I don’t know of any good workaround for the case where there's no namespace, but happily I haven’t run into that, so in practice that particular problem is probably moot.