From 0fd95d2e28dd739434ab3733d5bf86b2ff942035 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Mon, 7 Mar 2022 13:41:35 +0100 Subject: [PATCH] Fix ordered_map ctor with initializer_list (#3370) One of the ordered_map constructors was incorrectly accepting a std::initializer_list instead of std::initializer_list. Add regression test. Fixes #3343. --- include/nlohmann/ordered_map.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- test/src/unit-regression2.cpp | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index bfcf89a40..709bc5fd5 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -34,7 +34,7 @@ template , template ordered_map(It first, It last, const Allocator& alloc = Allocator()) : Container{first, last, alloc} {} - ordered_map(std::initializer_list init, const Allocator& alloc = Allocator() ) + ordered_map(std::initializer_list init, const Allocator& alloc = Allocator() ) : Container{init, alloc} {} std::pair emplace(const key_type& key, T&& t) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e40e3b05b..ec33a847a 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -17070,7 +17070,7 @@ template , template ordered_map(It first, It last, const Allocator& alloc = Allocator()) : Container{first, last, alloc} {} - ordered_map(std::initializer_list init, const Allocator& alloc = Allocator() ) + ordered_map(std::initializer_list init, const Allocator& alloc = Allocator() ) : Container{init, alloc} {} std::pair emplace(const key_type& key, T&& t) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 52db3589c..6249db63e 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -835,6 +835,18 @@ TEST_CASE("regression tests 2") CHECK(j.dump() == "[1,4]"); } + + SECTION("issue #3343 - json and ordered_json are not interchangable") + { + json::object_t jobj({ { "product", "one" } }); + ordered_json::object_t ojobj({{"product", "one"}}); + + auto jit = jobj.begin(); + auto ojit = ojobj.begin(); + + CHECK(jit->first == ojit->first); + CHECK(jit->second.get() == ojit->second.get()); + } } DOCTEST_CLANG_SUPPRESS_WARNING_POP