Skip to content

Commit

Permalink
Fix ordered_map ctor with initializer_list
Browse files Browse the repository at this point in the history
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 nlohmann#3343.
  • Loading branch information
falbrechtskirchinger committed Mar 3, 2022
1 parent e4643d1 commit 5d15829
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/nlohmann/ordered_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
template <class It>
ordered_map(It first, It last, const Allocator& alloc = Allocator())
: Container{first, last, alloc} {}
ordered_map(std::initializer_list<T> init, const Allocator& alloc = Allocator() )
ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
: Container{init, alloc} {}

std::pair<iterator, bool> emplace(const key_type& key, T&& t)
Expand Down
2 changes: 1 addition & 1 deletion single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17070,7 +17070,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
template <class It>
ordered_map(It first, It last, const Allocator& alloc = Allocator())
: Container{first, last, alloc} {}
ordered_map(std::initializer_list<T> init, const Allocator& alloc = Allocator() )
ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
: Container{init, alloc} {}

std::pair<iterator, bool> emplace(const key_type& key, T&& t)
Expand Down
12 changes: 12 additions & 0 deletions test/src/unit-regression2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>() == ojit->second.get<std::string>());
}
}

DOCTEST_CLANG_SUPPRESS_WARNING_POP

0 comments on commit 5d15829

Please sign in to comment.