Home > Backend Development > C++ > body text

How to Use std::min/max with #define NOMINMAX?

DDD
Release: 2024-11-12 07:46:02
Original
807 people have browsed it

How to Use std::min/max with #define NOMINMAX?

How to Use std::min/max with #define NOMINMAX

To use the standard library functions std::max and std::min while defining the macro NOMINMAX, follow these steps:

1. Define NOMINMAX in All Files Where std::min/max Are Used

To ensure the macro takes effect, you must define #define NOMINMAX in all files where you will use std::max and std::min. This prevents collisions with Windows' built-in max and min functions.

2. Use Parentheses to Bypass Macro Expansion

NOMINMAX defines std::min and std::max as macros. If you encounter the aforementioned errors, it means the preprocessor is interpreting these functions as macros. To bypass this, wrap the function names in parentheses:

(std::min)(x, y);  // For std::min
(std::max)(x, y);  // For std::max
Copy after login

By enclosing the function names in parentheses, the preprocessor will treat them as function calls rather than macro expansions.

Note: This solution is somewhat inconvenient, as it requires parenthesizing std::min and std::max every time you use them. However, it is a workaround for this particular issue.

The above is the detailed content of How to Use std::min/max with #define NOMINMAX?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template