简体   繁体   中英

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

#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.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:

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

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

As others mentioned, the errors are due to min/max macros that are defined in windows header(s). There are three ways of disabling them.

1) #define NOMINMAX before including header, this is generally a bad technique of defining macros in order to affect the following headers;

2) define NOMINMAX in compiler command line/IDE. The bad part about this decision is that if you want to ship your sources, you need to warn the users to do the same;

3) simply undefine the macros in your code before they are used

#undef min
#undef max

This is probably the most portable and flexible solution.

Try something like this:

#define NOMINMAX
#include <windows.h>

By default, windows.h defines min and max as macros. When those are expanded, code that tries to use std::min (for example) will end up looking something like this:

int k = std::(x) < (y) ? (x) : (y);

The error message is telling you that std::(x) isn't allowed.

In my case, project did not include windows.h or windef.h explicitly. It was using Boost. So, I resolved the issue by going to the project Properties -> C/C++ -> Preprocessor , and appending NOMINMAX in the Preprocessor Definitions (VS 2013, VS 2015).

For people including windows.h, put the following in effected headers:

#include windows headers ...

pragma push_macro("min")
pragma push_macro("max")
#undef min
#undef max

#include headers expecting std::min/std::max ...

...

pragma pop_macro("min")
pragma pop_macro("max")

In source files just #undef min and max.

#include windows headers ...

#undef min
#undef max

#include headers expecting std::min/std::max ...
#define NOMINMAX

is the trick to suppress the macro definitions of max and min

http://support.microsoft.com/kb/143208

To solve this issue I just create header file named fix_minmax.h without include guards

#ifdef max
    #undef max
#endif

#ifdef min
    #undef min
#endif

#ifdef MAX
    #undef MAX
#endif
#define MAX max

#ifdef MIN
   #undef MIN
#endif
#define MIN min

#include <algorithm>
using std::max;
using std::min;

Basic usage is like this.

// Annoying third party header with min/max macros
#include "microsoft-mega-api.h"
#include "fix_minmax.h"

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

I'd assume windows.h does define min as a macro, eg like

#define min(a,b)  ((a < b) ? a : b)

That would explain the error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM