Programming Tips - Win32: using min() / max() macros

Date: 2013jan2 OS: Windows Language: C/C++ Q. Win32: using min() / max() macros A. The windows.h defines min() and max() macros which are ok in most situations.
#include "windows.h" int smaller = min(a, b); int bigger = min(a, b);
But they aren't perfect since they are macros they might eval your parameters. Don't use so anything like min(a++, --b) So its better to use the STL functions:
#include <algorithm> int smaller = (std::min<int>)(a, b); int bigger = (std::min<int>)(a, b);
If you are using the STL function you might want to do this:
#define NOMINMAX #include <windows.h>
Which stops the min/max macros from being defined. Finally you can define it yourself:
template<class T> T min(T a, T b) { return a < b ? a : b; } template<class T> T max(T a, T b) { return a > b ? a : b; }