Skip to content

Commit 0fd95d2

Browse files
Fix ordered_map ctor with initializer_list (#3370)
One of the ordered_map constructors was incorrectly accepting a std::initializer_list<T> instead of std::initializer_list<value_type>. Add regression test. Fixes #3343.
1 parent c6d8892 commit 0fd95d2

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

include/nlohmann/ordered_map.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
3434
template <class It>
3535
ordered_map(It first, It last, const Allocator& alloc = Allocator())
3636
: Container{first, last, alloc} {}
37-
ordered_map(std::initializer_list<T> init, const Allocator& alloc = Allocator() )
37+
ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
3838
: Container{init, alloc} {}
3939

4040
std::pair<iterator, bool> emplace(const key_type& key, T&& t)

single_include/nlohmann/json.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17070,7 +17070,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
1707017070
template <class It>
1707117071
ordered_map(It first, It last, const Allocator& alloc = Allocator())
1707217072
: Container{first, last, alloc} {}
17073-
ordered_map(std::initializer_list<T> init, const Allocator& alloc = Allocator() )
17073+
ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
1707417074
: Container{init, alloc} {}
1707517075

1707617076
std::pair<iterator, bool> emplace(const key_type& key, T&& t)

test/src/unit-regression2.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,18 @@ TEST_CASE("regression tests 2")
835835

836836
CHECK(j.dump() == "[1,4]");
837837
}
838+
839+
SECTION("issue #3343 - json and ordered_json are not interchangable")
840+
{
841+
json::object_t jobj({ { "product", "one" } });
842+
ordered_json::object_t ojobj({{"product", "one"}});
843+
844+
auto jit = jobj.begin();
845+
auto ojit = ojobj.begin();
846+
847+
CHECK(jit->first == ojit->first);
848+
CHECK(jit->second.get<std::string>() == ojit->second.get<std::string>());
849+
}
838850
}
839851

840852
DOCTEST_CLANG_SUPPRESS_WARNING_POP

0 commit comments

Comments
 (0)