Skip to content

Commit 7973af7

Browse files
committed
fix: avoid copy-assign on const-key pairs in from_json for map types
std::transform + std::inserter on vector-backed ordered_map triggers element shifting code in GCC's <algorithm>, which requires pair::operator=. That is deleted when the key is const (e.g. ordered_map<string, string>). Replace with a range-for + emplace so no assignment is ever needed. Fix for the bug #5122
1 parent bc6bfc3 commit 7973af7

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

include/nlohmann/detail/conversions/from_json.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,10 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
386386

387387
ConstructibleObjectType ret;
388388
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
389-
using value_type = typename ConstructibleObjectType::value_type;
390-
std::transform(
391-
inner_object->begin(), inner_object->end(),
392-
std::inserter(ret, ret.begin()),
393-
[](typename BasicJsonType::object_t::value_type const & p)
389+
for (const auto& p : *inner_object)
394390
{
395-
return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
396-
});
391+
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
392+
}
397393
obj = std::move(ret);
398394
}
399395

0 commit comments

Comments
 (0)