Take the descent flag as an argument rather than testing it

MSVC reports the test of a constant as C4127 ("conditional expression is
constant"), which the Windows builds treat as an error: may_descend is
false for operator<, so the operand short-circuits the whole condition.

Passing it to compare_descent_exhausted() puts the test where the value
is an ordinary parameter, and leaves the call sites with no condition of
their own.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-08-21 08:50:20 +02:00
parent d5ebb3ed19
commit 5ee75cf96c
2 changed files with 24 additions and 10 deletions
+12 -5
View File
@@ -1233,15 +1233,22 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
compare_depth_guard& operator=(compare_depth_guard&&) = delete;
};
/// @brief whether a comparison must stop descending and finish iteratively
static bool compare_descent_exhausted() noexcept
/*!
@brief whether a comparison must stop descending and finish iteratively
@a may_descend says whether the operator descends at all; it is constant at
every call site, and is passed rather than tested by the caller so that the
test does not become a constant condition there, which MSVC reports (C4127).
*/
static bool compare_descent_exhausted(bool may_descend) noexcept
{
#ifdef JSON_NO_THREAD_LOCAL
// without a counter of its own per thread, the descent cannot be
// bounded without racing another one, so none is made
static_cast<void>(may_descend);
return true;
#else
return compare_depth() >= compare_depth_limit();
return !may_descend || compare_depth() >= compare_depth_limit();
#endif
}
@@ -4283,7 +4290,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
{ \
case value_t::array: \
{ \
if (JSON_HEDLEY_UNLIKELY(!(may_descend) || compare_descent_exhausted())) \
if (JSON_HEDLEY_UNLIKELY(compare_descent_exhausted(may_descend))) \
{ \
return (deep_result); \
} \
@@ -4293,7 +4300,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
\
case value_t::object: \
{ \
if (JSON_HEDLEY_UNLIKELY(!(may_descend) || compare_descent_exhausted())) \
if (JSON_HEDLEY_UNLIKELY(compare_descent_exhausted(may_descend))) \
{ \
return (deep_result); \
} \
+12 -5
View File
@@ -22587,15 +22587,22 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
compare_depth_guard& operator=(compare_depth_guard&&) = delete;
};
/// @brief whether a comparison must stop descending and finish iteratively
static bool compare_descent_exhausted() noexcept
/*!
@brief whether a comparison must stop descending and finish iteratively
@a may_descend says whether the operator descends at all; it is constant at
every call site, and is passed rather than tested by the caller so that the
test does not become a constant condition there, which MSVC reports (C4127).
*/
static bool compare_descent_exhausted(bool may_descend) noexcept
{
#ifdef JSON_NO_THREAD_LOCAL
// without a counter of its own per thread, the descent cannot be
// bounded without racing another one, so none is made
static_cast<void>(may_descend);
return true;
#else
return compare_depth() >= compare_depth_limit();
return !may_descend || compare_depth() >= compare_depth_limit();
#endif
}
@@ -25637,7 +25644,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
{ \
case value_t::array: \
{ \
if (JSON_HEDLEY_UNLIKELY(!(may_descend) || compare_descent_exhausted())) \
if (JSON_HEDLEY_UNLIKELY(compare_descent_exhausted(may_descend))) \
{ \
return (deep_result); \
} \
@@ -25647,7 +25654,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
\
case value_t::object: \
{ \
if (JSON_HEDLEY_UNLIKELY(!(may_descend) || compare_descent_exhausted())) \
if (JSON_HEDLEY_UNLIKELY(compare_descent_exhausted(may_descend))) \
{ \
return (deep_result); \
} \