Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

iostream

cin

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?

like image 526
Almo Avatar asked Jul 18 '12 14:07

Almo


People also ask

Does C have min and max?

The min and max functions are declared in the standard C++ algorithm header by the C++ Standard Template Library (STL). The fmin and fmax functions are included in the standard C math.

What does Max mean in C?

The max() function returns the greater of two values. The max() function is for C programs only.

Where is Min defined C?

std::min is defined in the header file <algorithm> and is used to find out the smallest of the number passed to it.

Does C have a built in MAX function?

max is an inline function (implemented using GNU C smart macros) which returns the greater of a and b. They may be any numeric values, either integer or floating point numbers, and they also may be pointers to the same base type.


1 Answers

Define the macro NOMINMAX:

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

like image 109
hmjd Avatar answered Oct 07 '22 02:10

hmjd