mirror of
https://github.com/nlohmann/json.git
synced 2026-08-22 17:13:18 +00:00
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:
@@ -1233,15 +1233,22 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
compare_depth_guard& operator=(compare_depth_guard&&) = delete;
|
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
|
#ifdef JSON_NO_THREAD_LOCAL
|
||||||
// without a counter of its own per thread, the descent cannot be
|
// without a counter of its own per thread, the descent cannot be
|
||||||
// bounded without racing another one, so none is made
|
// bounded without racing another one, so none is made
|
||||||
|
static_cast<void>(may_descend);
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
return compare_depth() >= compare_depth_limit();
|
return !may_descend || compare_depth() >= compare_depth_limit();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4283,7 +4290,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
{ \
|
{ \
|
||||||
case value_t::array: \
|
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); \
|
return (deep_result); \
|
||||||
} \
|
} \
|
||||||
@@ -4293,7 +4300,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
\
|
\
|
||||||
case value_t::object: \
|
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); \
|
return (deep_result); \
|
||||||
} \
|
} \
|
||||||
|
|||||||
@@ -22587,15 +22587,22 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
compare_depth_guard& operator=(compare_depth_guard&&) = delete;
|
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
|
#ifdef JSON_NO_THREAD_LOCAL
|
||||||
// without a counter of its own per thread, the descent cannot be
|
// without a counter of its own per thread, the descent cannot be
|
||||||
// bounded without racing another one, so none is made
|
// bounded without racing another one, so none is made
|
||||||
|
static_cast<void>(may_descend);
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
return compare_depth() >= compare_depth_limit();
|
return !may_descend || compare_depth() >= compare_depth_limit();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25637,7 +25644,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
{ \
|
{ \
|
||||||
case value_t::array: \
|
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); \
|
return (deep_result); \
|
||||||
} \
|
} \
|
||||||
@@ -25647,7 +25654,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
\
|
\
|
||||||
case value_t::object: \
|
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); \
|
return (deep_result); \
|
||||||
} \
|
} \
|
||||||
|
|||||||
Reference in New Issue
Block a user