Skip to content

Commit 93e49de

Browse files
Fix incomplete-type error in set_parents with ordered_json (#5167)
When iteration_proxy_value<iter_impl<ordered_json>> appears in a context that requires it to be complete (function or lambda parameter), the compiler instantiates basic_json<ordered_map> and walks into set_parents(iterator, typename iterator::difference_type) while iterator is still incomplete, failing with "invalid use of incomplete type". basic_json::difference_type is already std::ptrdiff_t, so just naming the underlying type directly avoids the dependent lookup. Behavior and ABI are unchanged. This was the approach suggested in the issue thread. Added a regression case in unit-ordered_json.cpp using the same trigger pattern (lambda parameter naming the proxy type). Fixes #3732 Signed-off-by: Akhilesh Arora <akhildawra@gmail.com>
1 parent a0a4e7c commit 93e49de

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

include/nlohmann/json.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,10 +741,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
741741
#endif
742742
}
743743

744-
iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
744+
iterator set_parents(iterator it, std::ptrdiff_t count_set_parents)
745745
{
746746
#if JSON_DIAGNOSTICS
747-
for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
747+
for (std::ptrdiff_t i = 0; i < count_set_parents; ++i)
748748
{
749749
(it + i)->m_parent = this;
750750
}

single_include/nlohmann/json.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21020,10 +21020,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
2102021020
#endif
2102121021
}
2102221022

21023-
iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
21023+
iterator set_parents(iterator it, std::ptrdiff_t count_set_parents)
2102421024
{
2102521025
#if JSON_DIAGNOSTICS
21026-
for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
21026+
for (std::ptrdiff_t i = 0; i < count_set_parents; ++i)
2102721027
{
2102821028
(it + i)->m_parent = this;
2102921029
}

tests/src/unit-ordered_json.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,15 @@ TEST_CASE("ordered_json")
6969
CHECK(oj1.size() == 4);
7070
CHECK(oj1.dump() == "{\"c\":1,\"b\":2,\"a\":3,\"d\":42}");
7171
}
72+
73+
TEST_CASE("regression test for issue #3732 - iteration_proxy_value<iter_impl<ordered_json>>")
74+
{
75+
// Naming the proxy type in a function-parameter position forces eager
76+
// instantiation of basic_json<ordered_map>; previously this hit an
77+
// incomplete-type error in set_parents().
78+
auto fn = [](nlohmann::detail::iteration_proxy_value<nlohmann::detail::iter_impl<nlohmann::ordered_json>> const& val)
79+
{
80+
return val.value();
81+
};
82+
static_cast<void>(fn);
83+
}

0 commit comments

Comments
 (0)