#include <algorithm>
#include <Windows.h>
int main()
{
int k = std::min(3, 4);
return 0;
}
What is windows doing if I include Windows.h? I can’t use std::min in visual studio 2005. The error message is:
error C2589: '(' : illegal token on right side of '::'
error C2059: syntax error : '::'
The
windows.hheader file (or more correctly,windef.hthat it includes in turn) has macros forminandmaxwhich are interfering.You should
#define NOMINMAXbefore including it.In fact, you should probably do that even if there were no conflict, since the naive definition of the macro shows why function-like macros are a bad idea:
If you invoke that macro with, for example:
then
ywill not end up with what you expect. For example, it will expand to:That expression (unless undefined behaviour kicks in which would be even worse) will decrement
ytwice, not something you’re likely to expect.I basically use macros only for conditional compilation nowadays, the other two major use cases of old (symbolic constants and function-like macros) are better handled with more modern language features (real enumerated types and inline function suggestion).