Home >Backend Development >C++ >Why Do I Get Errors When Using std::min/max with #define NOMINMAX?

Why Do I Get Errors When Using std::min/max with #define NOMINMAX?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-14 17:18:02334browse

Why Do I Get Errors When Using std::min/max with #define NOMINMAX?

Using std::min/max with #define NOMINMAX

In a recent update to your main.cpp file, you introduced the following preprocessor directive:

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

This action allows you to utilize the std::max and std::min functions within your code. However, subsequent attempts to employ these functions within other files yield errors such as:

error C2589: '(' : illegal token on right side of '::'
error C2059: syntax error : '::'

Despite attempts to define NOMINMAX in these additional files, the issue persists.

The issue lies in the fact that NOMINMAX defines aliases for the Windows min and max macros, overwriting the standard C versions provided by . To resolve this, use parentheses around the std::min and std::max calls:

(std::min)(x, y);

This approach avoids invoking the function-like macros, allowing the standard C versions to be applied.

The above is the detailed content of Why Do I Get Errors When Using std::min/max with #define NOMINMAX?. 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