Home > Article > Backend Development > Why is NOMINMAX not working in other files when using std::min and std::max?
NOMINMAX and std::min/max Collision
To enable usage of standard library's std::min and std::max functions, you've defined the NOMINMAX macro in your main.cpp file. However, you're facing issues using these functions in other files.
NOMINMAX is defined to disable certain legacy definitions in the Windows header files, including redefinitions of min and max as macros. This is because in earlier versions of the Visual C compiler, these macros were defined by Windows header files and could conflict with the standard library's definitions.
To resolve the issue, you need to ensure that NOMINMAX is defined in all the files where you intend to use std::min or std::max. However, simply adding the macro definition to your other files doesn't seem to work.
This is because #define directive has a global scope and affects all subsequent code in the file. However, macros are not applied to code that has already been preprocessed. Hence, the definition of NOMINMAX in your later files may have no effect on the code you've already written.
To work around this issue, you can enclose the function names with parentheses:
(std::min)(x, y);
This syntax will ensure that a function-like macro is not applied, and the compiler will resolve the function call to std::min.
The above is the detailed content of Why is NOMINMAX not working in other files when using std::min and std::max?. For more information, please follow other related articles on the PHP Chinese website!