Why does std::min(a, b) in a C++ logging header stop compiling when <windows.h> is included before it, and how do you fix it?
answer
- function-like macros named min and max
- expansion ignores the std:: qualifier
- only a following parenthesis triggers it
- define NOMINMAX before the include
basics
~20 s<windows.h> defines function-like macros named min and max unless NOMINMAX is defined first. The preprocessor expands min( even after std::, so the compiler sees std:: followed by an expression. Define NOMINMAX project-wide, or write (std::min)(a, b).
solid answer
~40 sBy default `<windows.h>` defines **function-like macros** `min(a, b)` and `max(a, b)`. A function-like macro expands wherever its name is followed by `(`, and the preprocessor ignores the C++ `std::` before it, so `std::min(level, 5)` becomes `std::` followed by a conditional expression and clang reports `expected unqualified-id`. `std::numeric_limits<int>::max()` breaks too, with `too few arguments provided to function-like macro invocation`. The robust fix is to define `NOMINMAX` before `<windows.h>` is included, ideally as a project-wide compile definition so include order stops mattering. Inside a shared header you do not control, write `(std::min)(a, b)` or `(std::numeric_limits<int>::max)()`: with a `)` after the name, no function-like expansion happens. `std::min<int>(a, b)` also works, because `<` follows the name. `#undef min` after the include is fragile, since later code may expect the macros.
code
cpp · 21 lines// Stand-in for what <windows.h> defines unless NOMINMAX is defined first.
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
#include <algorithm>
#include <cstdio>
#include <limits>
namespace logx {
inline int clamp_level(int level) {
#ifdef SHOW_ERROR
return std::max(0, std::min(level, 5)); // expands the macros: does not compile
#else
return (std::max)(0, (std::min)(level, 5)); // parentheses stop function-like expansion
#endif
}
} // namespace logx
int main() {
std::printf("%d %d\n", logx::clamp_level(9), (std::numeric_limits<int>::max)());
}go deeper
Recall that <windows.h> defines min and max macros and that defining NOMINMAX before including it turns them off.
Explain the mechanism: a function-like macro expands wherever its name is followed by a parenthesis, regardless of std:: or class qualification, and why (std::min)(a, b) escapes it.
Show how you make a shared header immune to include order, prefer a project-wide definition, and recognise the same class of bug in any unprefixed lower-case macro.
Turn it into a rule: third-party headers that define macros get wrapped or configured centrally, and the codebase's own macros are upper-case and prefixed so they cannot collide.
## The scenario A cross-platform logging header, `logx.hpp`, clamps a level with `std::min(level, 5)` and prints `std::numeric_limits<int>::max()` in a diagnostic. It builds everywhere until a Windows source file includes `<windows.h>` before `logx.hpp`. Then the header stops compiling, with errors pointing at lines nobody changed. ## Why it breaks Unless told otherwise, the Windows SDK's `<windows.h>` defines two **function-like macros**, `min(a, b)` and `max(a, b)`. Two preprocessor rules then collide with ordinary C++: 1. A function-like macro is expanded wherever its name is **immediately followed by `(`**, after any whitespace. The name alone, or the name followed by anything else, is left untouched. 2. The preprocessor knows nothing about namespaces or classes. `std::min(` and `numeric_limits<int>::max(` both contain the macro name followed by `(`, so both expand. After expansion, `std::min(level, 5)` reads as `std::` followed by a parenthesized conditional, which is not a name. With stand-in function-like definitions of `min` and `max`, Apple clang reports: - `expected unqualified-id` for `std::max(0, std::min(level, 5))`; - `too few arguments provided to function-like macro invocation` for `std::numeric_limits<int>::max()`, because `max` was given zero arguments instead of two. The same failure hits any identifier the macros match: a member function `Range::max()`, C++20's `std::ranges::min(a, b)` (the token `min` is still followed by `(`, and clang again reports `expected unqualified-id`), and so on. Even where the macro does expand into valid code, as in an unqualified `min(next(), limit)`, it is a function-like macro, so an argument with side effects may be evaluated twice. ## The fixes, from best to last resort | Fix | Where | Why it works | Caveat | |---|---|---|---| | define `NOMINMAX` before `<windows.h>` | a project-wide compile definition | the header then skips defining `min` and `max` | every translation unit must see it before the include | | `(std::min)(a, b)` | at each call | the name is followed by `)`, not `(` | slightly odd-looking | | `std::min<int>(a, b)` | at each call | the name is followed by `<` | fixes the type explicitly | | `#undef min` and `#undef max` | after the include | removes the macros | breaks code that expects them | | `#pragma push_macro` / `pop_macro` | around your header | saves and restores the macros | a compiler extension, not standard C++ | A few details matter in practice: - Setting `NOMINMAX` as a compile definition for the whole target is better than writing `#define NOMINMAX` above one include, because a different file may include `<windows.h>` first. Test frameworks do this themselves: Catch2's Windows header wrapper and Google Benchmark define `NOMINMAX`, guarded by `#ifndef`, before they include `<windows.h>`. - A shared header that must survive any include order should use the parenthesized form for every `min`, `max` and `numeric_limits<T>::max()` call. The parentheses change nothing about the meaning; `(std::min)(a, b)` still calls `std::min`, and the initializer-list overload works the same way: `(std::max)({a, b, c})`. - Standard library implementations protect their own headers. libc++, for example, saves `min` and `max` with `push_macro`, undefines them while its own code is parsed, and restores them afterwards, so `#include <algorithm>` after `<windows.h>` is safe. That protection ends at the library's last line; your own calls are still expanded. ## The general lesson about macros in C++ The bug has nothing to do with Windows specifically; it is what any **lower-case, unprefixed function-like macro** does to C++ code. Macros ignore namespaces, so a common name like `min`, `max`, `check` or `assert_true` in one header can break unrelated code in another. When you must write a macro, give it an **upper-case, project-prefixed** name such as `LOGX_CHECK`, and prefer a function or template wherever the job is about values rather than source text. ## Diagnosing it quickly 1. Look for an error that makes no sense at a `std::min`, `std::max` or `::max()` call. 2. Preprocess the file (`-E`) and look at the line: the call will have turned into a conditional expression. 3. Search the include chain for the header that defines the macro, then apply the fixes above.
- Why is a project-wide NOMINMAX compile definition better than #define NOMINMAX at the top of one file?The macro only helps if it is defined before the first inclusion of <windows.h> in each translation unit. A #define in one file misses every other file, and even in that file a header included earlier may already have pulled in <windows.h>. A compile definition is seen first in every translation unit, so include order stops mattering.
- Why does (std::min)(a, b) still call the same function as std::min(a, b)?Parentheses around a name in a call do not change which entity the name denotes; they only stop a function-like macro from matching, because the macro name is followed by ) instead of (. For std::min the qualified name already disables argument-dependent lookup, so the result is identical once no macro is involved.
- The std:: qualifier protects std::min from a macro named min
- Including <algorithm> after <windows.h> fixes it, because the standard header wins
- The fix is to stop using std::min and write the comparison by hand everywhere
- #undef min right after the include is always safe
- Only free functions are affected; member functions such as max() cannot be