From fb8c11f25cb6ff4abc305d988a609751726e674e Mon Sep 17 00:00:00 2001 From: gatopeich Date: Tue, 23 Jun 2020 15:01:20 +0100 Subject: [PATCH] Re-implement ordered_map::erase, so that it can handle pair --- include/nlohmann/json.hpp | 6 +++--- include/nlohmann/ordered_map.hpp | 9 ++++++++- single_include/nlohmann/json.hpp | 15 +++++++++++---- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 31944e32..2aef23b3 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -499,9 +499,9 @@ class basic_json // Note the use of std::map default allocator as a placeholder // to extract the actual ObjectType::value_type AllocatorType> - >::value_type>>; + ObjectType> + >::value_type>>; /*! @brief a type for an array diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 756b586a..bb5dfe4e 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -46,7 +46,14 @@ struct ordered_map : Container { if (it->first == key) { - Container::erase(it); + // Since we cannot move const Keys, re-construct them in place + for (auto next = it; ++next != this->end(); ++it) + { + // *it = std::move(*next); // deleted + it->~value_type(); // Destroy but keep allocation + new (&*it) value_type{std::move(*next)}; + } + Container::pop_back(); return 1; } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 76ee2764..3cf133d5 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15915,7 +15915,14 @@ struct ordered_map : Container { if (it->first == key) { - Container::erase(it); + // Since we cannot move const Keys, re-construct them in place + for (auto next = it; ++next != this->end(); ++it) + { + // *it = std::move(*next); // deleted + it->~value_type(); // Destroy but keep allocation + new (&*it) value_type{std::move(*next)}; + } + Container::pop_back(); return 1; } } @@ -16351,9 +16358,9 @@ class basic_json // Note the use of std::map default allocator as a placeholder // to extract the actual ObjectType::value_type AllocatorType> - >::value_type>>; + ObjectType> + >::value_type>>; /*! @brief a type for an array