From 8557b661abce264d5e7de6da193c40bf47c2469c Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 10 Jul 2026 07:37:26 +0200 Subject: [PATCH] Fix MSVC C4127 warning-as-error in is_exactly_representable_as_float MSVC's /W4 flags 'if (float_digits >= int_digits)' as C4127 (conditional expression is constant), since both operands are constexpr int for any single template instantiation. This CI job builds with warnings-as-errors, failing the build. Apply the same MSVC-only pragma push/disable(4127)/pop pattern already used elsewhere in the codebase (see json.hpp's set_parents() workaround) rather than if constexpr, since this file must stay C++11-compatible. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/hash.hpp | 7 +++++++ single_include/nlohmann/json.hpp | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/include/nlohmann/detail/hash.hpp b/include/nlohmann/detail/hash.hpp index 4b845096a..9df26dcb8 100644 --- a/include/nlohmann/detail/hash.hpp +++ b/include/nlohmann/detail/hash.hpp @@ -40,10 +40,17 @@ inline bool is_exactly_representable_as_float(typename BasicJsonType::number_int constexpr int float_digits = std::numeric_limits::digits; constexpr int int_digits = std::numeric_limits::digits; +#ifdef JSON_HEDLEY_MSVC_VERSION +#pragma warning(push ) +#pragma warning(disable : 4127) // ignore warning to replace if with if constexpr +#endif if (float_digits >= int_digits) { return true; } +#ifdef JSON_HEDLEY_MSVC_VERSION +#pragma warning( pop ) +#endif // For values outside float's exact range, they don't round-trip // The safe way to check: compute the max magnitude that round-trips diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index fb1bbb6b6..4b233ed5c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6708,10 +6708,17 @@ inline bool is_exactly_representable_as_float(typename BasicJsonType::number_int constexpr int float_digits = std::numeric_limits::digits; constexpr int int_digits = std::numeric_limits::digits; +#ifdef JSON_HEDLEY_MSVC_VERSION +#pragma warning(push ) +#pragma warning(disable : 4127) // ignore warning to replace if with if constexpr +#endif if (float_digits >= int_digits) { return true; } +#ifdef JSON_HEDLEY_MSVC_VERSION +#pragma warning( pop ) +#endif // For values outside float's exact range, they don't round-trip // The safe way to check: compute the max magnitude that round-trips