Why is std::min failing when windows.h is included?

The windows.h header file (or more correctly, windef.h that it includes in turn) has macros for min and max which are interfering.

You should #define NOMINMAX before including it.


No need to define anything, just bypass the macro using this syntax:

(std::min)(a, b); // added parentheses around function name
(std::max)(a, b);

I still have trouble occasionally with the windows headers and project wide define of NOMINMAX doesn't always seem to work. As an alternative to using parentheses, I sometimes make the type explicit like so:

int k = std::min<int>(3, 4);

This also stops the preprocessor from matching to min and is arguably more readable than the parentheses workaround.