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>
This commit is contained in:
Akhilesh Arora
2026-05-14 08:52:39 +02:00
committed by GitHub
parent a0a4e7cc0b
commit 93e49decbd
3 changed files with 16 additions and 4 deletions
+2 -2
View File
@@ -741,10 +741,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
#endif
}
iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
iterator set_parents(iterator it, std::ptrdiff_t count_set_parents)
{
#if JSON_DIAGNOSTICS
for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
for (std::ptrdiff_t i = 0; i < count_set_parents; ++i)
{
(it + i)->m_parent = this;
}
+2 -2
View File
@@ -21020,10 +21020,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
#endif
}
iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
iterator set_parents(iterator it, std::ptrdiff_t count_set_parents)
{
#if JSON_DIAGNOSTICS
for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
for (std::ptrdiff_t i = 0; i < count_set_parents; ++i)
{
(it + i)->m_parent = this;
}
+12
View File
@@ -69,3 +69,15 @@ TEST_CASE("ordered_json")
CHECK(oj1.size() == 4);
CHECK(oj1.dump() == "{\"c\":1,\"b\":2,\"a\":3,\"d\":42}");
}
TEST_CASE("regression test for issue #3732 - iteration_proxy_value<iter_impl<ordered_json>>")
{
// Naming the proxy type in a function-parameter position forces eager
// instantiation of basic_json<ordered_map>; previously this hit an
// incomplete-type error in set_parents().
auto fn = [](nlohmann::detail::iteration_proxy_value<nlohmann::detail::iter_impl<nlohmann::ordered_json>> const& val)
{
return val.value();
};
static_cast<void>(fn);
}