Fix MSVC C4127 in the lookups of editable json documents

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-11 10:28:39 +02:00
1 parent ae45c369c8
commit 6b9328b6ab
2 files changed
+16 -3

No files matched your search

@@ -231,6 +231,13 @@ struct document_data
template<bool Editable> template<bool Editable>
struct navigation 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 static NLOHMANN_VIEW_ALWAYS_INLINE const node* first(const document_data& /*d*/, const node* n) noexcept
{ {
return n + 1; return n + 1;
@@ -250,6 +257,11 @@ struct navigation
template<> template<>
struct navigation<true> struct navigation<true>
{ {
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 static NLOHMANN_VIEW_ALWAYS_INLINE const node* first(const document_data& d, const node* n) noexcept
{ {
return d.first_child_edited(n); return d.first_child_edited(n);
+4 -3
View File
@@ -92,7 +92,7 @@ template<bool Editable>
const node* find_member(const document_data& d, const node* object, const char* key, std::size_t n) noexcept const node* find_member(const document_data& d, const node* object, const char* key, std::size_t n) noexcept
{ {
using nav = navigation<Editable>; using nav = navigation<Editable>;
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) return find_indexed(d, object, key, n); // a large object (whose members have not been edited)
} }
@@ -125,8 +125,9 @@ const node* find_member(const document_data& d, const node* object, const char*
template<bool Editable> template<bool Editable>
const node* element_at(const document_data& d, const node* array, std::size_t idx) noexcept const node* element_at(const document_data& d, const node* array, std::size_t idx) noexcept
{ {
const node* e = navigation<Editable>::first(d, array); using nav = navigation<Editable>;
if (Editable && (array->flags & node_flags::moved) != 0 && d.edits->moved_cap[array->off] != 0) 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 return e + idx; // a growable block: one link per element
} }