From 30d8ddd65aba303cf746ca3b06bf59d01349e392 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 9 Oct 2026 23:48:40 +0200 Subject: [PATCH] Fix MSVC C4127 in the lookups of editable json documents Signed-off-by: Niels Lohmann --- include/nlohmann/detail/view/document_data.hpp | 12 ++++++++++++ include/nlohmann/detail/view/lookup.hpp | 7 ++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/nlohmann/detail/view/document_data.hpp b/include/nlohmann/detail/view/document_data.hpp index 2e1eac3ef..dd2c6be75 100644 --- a/include/nlohmann/detail/view/document_data.hpp +++ b/include/nlohmann/detail/view/document_data.hpp @@ -217,6 +217,13 @@ struct document_data template struct navigation { + /// whether the walk follows edits (the template argument as a runtime + /// condition: `Editable && ...` is a constant condition for MSVC, C4127) + static NLOHMANN_VIEW_ALWAYS_INLINE bool editable() noexcept + { + return false; + } + static NLOHMANN_VIEW_ALWAYS_INLINE const node* first(const document_data& /*d*/, const node* n) noexcept { return n + 1; @@ -236,6 +243,11 @@ struct navigation template<> struct navigation { + static NLOHMANN_VIEW_ALWAYS_INLINE bool editable() noexcept + { + return true; + } + static NLOHMANN_VIEW_ALWAYS_INLINE const node* first(const document_data& d, const node* n) noexcept { return d.first_child_edited(n); diff --git a/include/nlohmann/detail/view/lookup.hpp b/include/nlohmann/detail/view/lookup.hpp index afc43e95a..5bfe36b94 100644 --- a/include/nlohmann/detail/view/lookup.hpp +++ b/include/nlohmann/detail/view/lookup.hpp @@ -92,7 +92,7 @@ template const node* find_member(const document_data& d, const node* object, const char* key, std::size_t n) noexcept { using nav = navigation; - if (NLOHMANN_VIEW_UNLIKELY(object->extra != 0) && (!Editable || (object->flags & node_flags::moved) == 0)) + if (NLOHMANN_VIEW_UNLIKELY(object->extra != 0) && (!nav::editable() || (object->flags & node_flags::moved) == 0)) { return find_indexed(d, object, key, n); // a large object (whose members have not been edited) } @@ -143,8 +143,9 @@ SizeType to_index(IntegerType idx) noexcept template const node* element_at(const document_data& d, const node* array, std::size_t idx) noexcept { - const node* e = navigation::first(d, array); - if (Editable && (array->flags & node_flags::moved) != 0 && d.edits->moved_cap[array->off] != 0) + using nav = navigation; + const node* e = nav::first(d, array); + if (nav::editable() && (array->flags & node_flags::moved) != 0 && d.edits->moved_cap[array->off] != 0) { return e + idx; // a growable block: one link per element }