Possible Problems with Nominmax on Visual C++

Possible problems with NOMINMAX on Visual C++

Using NOMINMAX is the only not-completely-evil way to include <windows.h>. You should also define UNICODE and STRICT. Although the latter is defined by default by modern implementations.

You can however run into problems with Microsoft’s headers, e.g. for GdiPlus. I’m not aware of problems with headers from any other companies or persons.

If the header defines a namespace, as GdiPlus does, then one fix is to create a wrapper for the relevant header, where you include <algorithm>, and inside the header’s namespace, using namespace std; (or alternatively using std::min; and using std::max):

#define NOMINMAX
#include <algorithm>
namespace Gdiplus
{
using std::min;
using std::max;
}

Note that that is very different from a using namespace std; at global scope in header, which one should never do.

I don’t know of any good workaround for the case where there's no namespace, but happily I haven’t run into that, so in practice that particular problem is probably moot.

#define NOMINMAX using std::min/max

If you're really desperate, put parentheses around the function names:

(std::min)(x, y);

This syntax won't apply a function-like macro. (Formally, to apply a function-like macro the name of the macro must be followed by optional white space then a '('.)

NOMINMAX with Visual Studio 2012 MFC project

I don't work on Windows so I'm not used to dealing with this, and I'm not testing this, but I believe that answer is suggesting you do this:

#define NOMINMAX
#include <algorithm>
namespace Gdiplus
{
using std::min;
using std::max;
};
//... your other includes.

This will get the "proper" versions of min and max, and make them available without the std:: prefix (which seems to be how it is used in the GdiplusTypes.h header).

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

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.


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:

#define max(a,b) ((a)>(b)?(a):(b))

If you invoke that macro with, for example:

int x = 5, y = 10;
int c = max(x++, y--);

then y will not end up with what you expect. For example, it will expand to:

int c = ((x++)>(y--)?(x++):(y--));

That expression (unless undefined behaviour kicks in which would be even worse) will decrement y twice, 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).

MIN/MAX Macros not working with visual studio

That typeof stuff is gcc specific. You don't have that stuff when compiling with cl.exe (microsoft's compiler that visual studio uses). https://social.msdn.microsoft.com/Forums/vstudio/en-US/984ae3e8-6391-45b9-8885-edb088da8bfa/will-msvc-support-a-typeof-operator-like-in-gcc?forum=vclanguage

Problem calling std::max

You are probably including windows.h somewhere, which defines macros named max and min.

You can #define NOMINMAX before including windows.h to prevent it from defining those macros, or you can prevent macro invocation by using an extra set of parentheses:

column = (std::max)(1u, column + count);

How do I deal with the max macro in windows.h colliding with max in std?

Define the macro NOMINMAX:

This will suppress the min and max definitions in Windef.h.

warning C4003 and errors C2589 and C2059 on: x = std::numeric_limitsint::max();

This commonly occurs when including a Windows header that defines a min or max macro. If you're using Windows headers, put #define NOMINMAX in your code, or build with the equivalent compiler switch (i.e. use /DNOMINMAX for Visual Studio).

Note that building with NOMINMAX disables use of the macro in your entire program. If you need to use the min or max operations, use std::min() or std::max() from the <algorithm> header.

Syntax error with std::numeric_limits::max

Your problem is caused by the <Windows.h> header file that includes macro definitions named max and min:

#define max(a,b) (((a) > (b)) ? (a) : (b))

Seeing this definition, the preprocessor replaces the max identifier in the expression:

std::numeric_limits<size_t>::max()

by the macro definition, eventually leading to invalid syntax:

std::numeric_limits<size_t>::(((a) > (b)) ? (a) : (b))

reported in the compiler error: '(' : illegal token on right side of '::'.

As a workaround, you can add the NOMINMAX define to compiler flags (or to the translation unit, before including the header):

#define NOMINMAX   

or wrap the call to max with parenthesis, which prevents the macro expansion:

size_t maxValue_ = (std::numeric_limits<size_t>::max)()
// ^ ^

or #undef max before calling numeric_limits<size_t>::max():

#undef max
...
size_t maxValue_ = std::numeric_limits<size_t>::max()


Related Topics



Leave a reply



Submit