Home  >  Article  >  Backend Development  >  How to Resolve Macro Collisions between Windows.h and the C Standard Library?

How to Resolve Macro Collisions between Windows.h and the C Standard Library?

Susan Sarandon
Susan SarandonOriginal
2024-11-17 04:42:03183browse

How to Resolve Macro Collisions between Windows.h and the C   Standard Library?

Handling Macro Collisions in Windows.h and std

When working with Windows APIs and the C standard library, you may encounter conflicts between macro definitions, such as max. This can occur if Windows.h is included, which defines max(), and subsequently the C standard library is included, which also defines max().

One workaround is to redefine the conflicting macros, as suggested in the code snippet you provided (#undef max). However, this can be cumbersome and error-prone. A better solution is to suppress the definition of max() and related macros in Windows.h. This can be achieved by defining the preprocessor macro NOMINMAX.

#define NOMINMAX

#include <Windows.h>
#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);
}

By defining NOMINMAX before including Windows.h, the macro definitions for max(), min(), and others are suppressed, allowing the C standard library's versions of these macros to be used without conflicts.

The above is the detailed content of How to Resolve Macro Collisions between Windows.h and the C 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