Edmund's blog

Writing code.

Work around min and max macros

Here is another trick that I learned for Stephan Lavavej’s posts on reddit.

How can you work around those evil lowercase min and max macros that Microsoft defines, so that you can use the std::min and std::max template functions? Well, it turns out that you can put parentheses around them. Not beautiful, but it works.

int val1 = 13;
int val2 = 10;
int minVal = (std::min)(val1, val2);

Obviously, the best solution is to define NOMINMAX in your precompiled headers file.

#define NOMINMAX