#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 cant 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.h
header file (or more correctly,windef.h
that it includes in turn) has macros formin
andmax
which are interfering.You should
#define NOMINMAX
before including it.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:
This also stops the preprocessor from matching to
min
and is arguably more readable than the parentheses workaround.Try something like this:
By default, windows.h defines
min
andmax
as macros. When those are expanded, code that tries to usestd::min
(for example) will end up looking something like this:The error message is telling you that
std::(x)
isn't allowed.For people including windows.h, put the following in effected headers:
In source files just #undef min and max.
I'd assume windows.h does define min as a macro, e.g. like
That would explain the error message.
To solve this issue I just create header file named
fix_minmax.h
without include guardsBasic usage is like this.
Pros of this approach is that it works with every kind of included file or part of code. This also saves your time when dealing with code or libraries that depend on
min
/max
macros