From 2b2f392d188e447752a75bbe4546a25bf64a3d1c Mon Sep 17 00:00:00 2001 From: Tom Lachecki Date: Thu, 7 Sep 2023 13:44:57 +0100 Subject: [PATCH] Fixed init-list construction when size_type is not int --- include/nlohmann/json.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index a3c0af1e00..d9163c3575 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -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& 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(0)].is_string(); }); // adjust type if type deduction is not wanted