Home > Article > Backend Development > How to Resolve the Windows.h Max Macro Collision with std::numeric_limits::max()?
Windows.h Max Macro Collision with Std: A Solution
When developing with C , conflicts between standard library definitions and those found in the Windows.h header can arise. This collision can affect the use of the max macro.
Specifically, the code snippet provided attempts to handle invalid user input from cin using a max macro. However, on Windows, the max macro from Windows.h conflicts with the std::numeric_limits
To resolve this issue, a cleaner and more elegant approach is to suppress the min and max definitions in Windef.h. This can be achieved by defining the NOMINMAX macro before including Windows.h.
#define NOMINMAX #include <Windows.h> // includes WinDef.h which defines min() max() 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);
By defining NOMINMAX, you effectively disable the conflicting min and max definitions in Windows.h, allowing the use of the std::numeric_limits
The above is the detailed content of How to Resolve the Windows.h Max Macro Collision with std::numeric_limits::max()?. For more information, please follow other related articles on the PHP Chinese website!