std::min gives error

2019-01-07 06:45发布

问题:

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

回答1:

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.



回答2:

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);


回答3:

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.



回答4:

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:

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

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



回答5:

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.



回答6:

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).



回答7:

#define NOMINMAX

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

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



回答8:

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


回答9:

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

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

That would explain the error message.



回答10:

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