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

C++IostreamCin

C++ Problem Overview


So I was trying to get valid integer input from cin, and used an answer to this question.

It recommended:

#include <Windows.h> // includes WinDef.h which defines min() max()
#include <iostream>
using std::cin;
using std::cout;

void Foo()
{
    int delay = 0;
    do
    {
        if(cin.fail())
        {
            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
        cout << "Enter number of seconds between submissions: ";
    } while(!(cin >> delay) || delay == 0);
}

Which gives me an error on Windows, saying that the max macro doesn't take that many arguments. Which means I have to do this

do
{
    if(cin.fail())
    {
        cin.clear();
#undef max
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    cout << "Enter number of seconds between submissions: ";
} while(!(cin >> delay) || delay == 0);

To get it to work. That's pretty ugly; is there a better way to work around this issue? Maybe I should be storing the definition of max and redefining it afterward?

C++ Solutions


Solution 1 - C++

Define the macro NOMINMAX:

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

Solution 2 - C++

Just wrap the function name in parenthesis:

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

No need for the NOMINMAX macro in this case, plus you won't get compiler warnings

Solution 3 - C++

If you don't know whether somebody else might have included windows.h without NOMINMAX, you might define a dummy macro which can be used to suppress function-like macro invocations without changing the definition:

#define DUMMY
...
std::numeric_limits<std::streamsize>::max DUMMY ()

Not really pretty either, but works and is non-intrusive.

When working with the Windows header file, I prefer to hide it as much as I can by including it only in specialized code and header files (using pimpl if necessary), because it throws just too much garbage into the global namespace.

Solution 4 - C++

Are you just trying to flush the cin buffer? I always just used:

cin.ignore(cin.rdbuf()->in_avail());

Solution 5 - C++

If you happen to use GDI+, the approach with NOMINMAX won't work for you, because headers of GDI+ require min or max in global namespace.

And the simplest workaround in this case is to undefine min/max when they are no longer needed.

The code sample to illustrate the approach:

//#define NOMINMAX - this won't work
#include <Windows.h>
#include <gdiplus.h>
#undef max
#undef min
...
#include <cxxopts.hpp>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlmoView Question on Stackoverflow
Solution 1 - C++hmjdView Answer on Stackoverflow
Solution 2 - C++NIROView Answer on Stackoverflow
Solution 3 - C++PhilippView Answer on Stackoverflow
Solution 4 - C++LittlegatorView Answer on Stackoverflow
Solution 5 - C++AntonKView Answer on Stackoverflow