Home >Backend Development >C++ >How to Resolve the `max` Macro Collision Between Windows.h and the Standard Library?

How to Resolve the `max` Macro Collision Between Windows.h and the Standard Library?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-11 16:41:02231browse

How to Resolve the `max` Macro Collision Between Windows.h and the Standard Library?

Min and Max Macro Collision Between Windows.h and Std

When dealing with input validation in C , one may encounter a conflict between the max macro defined in Windows.h and the max function in the standard library. This can arise when attempting to clear the input stream error flag using the ignore function.

To resolve this issue, consider using the following approach:

#define NOMINMAX
#include <Windows.h>
#include <iostream>

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

The NOMINMAX macro suppresses the min and max definitions in Windows.h, allowing the program to use the max function from the standard library without conflict. This approach eliminates the need to manually undefine the max macro and redefine it later.

The above is the detailed content of How to Resolve the `max` Macro Collision Between Windows.h and the Standard Library?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn