Windows max macro breaks std::numeric_limits<T>::max() in headers
PitfallVerified 29 Sep 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.
Symptom#
A C++ header that uses std::numeric_limits<T>::max() fails to compile when Windows code includes it, because the Windows max macro expands and breaks the expression.
Cause#
Windows headers define max (and min) as macros. In a header included by Windows code, std::numeric_limits<T>::max() is expanded by the macro.
Fix#
Write (std::numeric_limits<T>::max)() with extra parentheses around the function name. The parentheses stop the macro expansion. Match this form in all headers Windows code may include.
Evidence#
2026-09-30 session in dftracer-utils: the project headers already use (std::numeric_limits<T>::max)() for this reason.
Also found by searching
- error C2589: '(': illegal token on right side of '::' std::numeric_limits<int>::max()
- header using numeric_limits max fails to compile on Windows MSVC
- how do I use std::numeric_limits<T>::max() in a header included with windows.h
- windows.h min max macro conflict, NOMINMAX or parentheses around max
- error C2589: '(': illegal token on right side of '::' std::numeric_limits<size_t>::max() MSVC
- windows.h min max macros break my header, numeric_limits max() fails to compile only on Windows
- how do I use std::numeric_limits<T>::max() in a header safely when windows.h is included?
- NOMINMAX alternative: why wrap max in parentheses, macro expansion error in cross-platform C++ library