Fix old clang and MSVC C4127 in editable json documents

Value-initialize the const std::less in find_parent (clang 3.4/3.6 do not
implement DR 253), and test the Editable template argument through a
function to avoid MSVC C4127.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-11 10:28:31 +02:00
1 parent 671b589d71
commit 4d9e032d7d
2 files changed
+10 -2

No files matched your search

@@ -177,7 +177,7 @@ inline node* block_of(document_data& d, node* n, std::size_t extra)
/// still covers its original subtree. /// still covers its original subtree.
inline node* find_parent(const document_data& d, const node* target) inline node* find_parent(const document_data& d, const node* target)
{ {
const std::less<const node*> lt; const std::less<const node*> lt{};
const node* lo = d.tape; const node* lo = d.tape;
const node* hi = d.tape + d.tape_size; const node* hi = d.tape + d.tape_size;
const node* c = d.tape; const node* c = d.tape;
+9 -1
View File
@@ -729,11 +729,19 @@ class basic_json_view
return detail::view::view_side<BasicJsonType, basic_json_view>(v); return detail::view::view_side<BasicJsonType, basic_json_view>(v);
} }
/// the template argument as a runtime condition: testing it directly in
/// `Editable && ...` makes the condition constant, which MSVC reports as
/// C4127
static bool editable() noexcept
{
return Editable;
}
/// the number of source bytes of this value (estimated for values with /// the number of source bytes of this value (estimated for values with
/// decoded strings); the estimate sizes the output buffer of dump() /// decoded strings); the estimate sizes the output buffer of dump()
std::size_t source_extent() const noexcept std::size_t source_extent() const noexcept
{ {
if (Editable && m_doc->edits != nullptr) if (editable() && m_doc->edits != nullptr)
{ {
// positions of moved and new values are not source offsets // positions of moved and new values are not source offsets
return m_node == m_doc->tape ? m_doc->size + m_doc->edits->text_used : 64; return m_node == m_doc->tape ? m_doc->size + m_doc->edits->text_used : 64;