Skip to content

Commit 6e36c72

Browse files
Fixed init-list construction when size_type is not int (#4140)
1 parent e75b94b commit 6e36c72

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

include/nlohmann/json.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
906906
bool is_an_object = std::all_of(init.begin(), init.end(),
907907
[](const detail::json_ref<basic_json>& element_ref)
908908
{
909-
return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[0].is_string();
909+
// The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
910+
// (many string types can be constructed from 0 via its null-pointer guise, so we get a
911+
// broken call to op[key_type], the wrong semantics and a 4804 warning on Windows)
912+
return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
910913
});
911914

912915
// adjust type if type deduction is not wanted

single_include/nlohmann/json.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20112,7 +20112,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
2011220112
bool is_an_object = std::all_of(init.begin(), init.end(),
2011320113
[](const detail::json_ref<basic_json>& element_ref)
2011420114
{
20115-
return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[0].is_string();
20115+
// The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
20116+
// (many string types can be constructed from 0 via its null-pointer guise, so we get a
20117+
// broken call to op[key_type], the wrong semantics and a 4804 warning on Windows)
20118+
return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
2011620119
});
2011720120

2011820121
// adjust type if type deduction is not wanted

0 commit comments

Comments
 (0)