Skip to content

Commit

Permalink
Fixed init-list construction when size_type is not int
Browse files Browse the repository at this point in the history
  • Loading branch information
tomalakgeretkal committed Sep 8, 2023
1 parent 788e546 commit 2b2f392
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
bool is_an_object = std::all_of(init.begin(), init.end(),
[](const detail::json_ref<basic_json>& element_ref)
{
return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[0].is_string();
// The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
// (many string types can be constructed from 0 via its null-pointer guise, so we get a
// broken call to op[key_type], the wrong semantics and a 4804 warning on Windows)
return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
});

// adjust type if type deduction is not wanted
Expand Down

0 comments on commit 2b2f392

Please sign in to comment.