Skip to content

Using iteration_proxy_value with ordered_json fails to compile due to incomplete type #3732

Closed

Description

@falbrechtskirchinger

Description

Using iteration_proxy_value with ordered_json as shown below fails to compile due to an incomplete type error in iterator set_parents(iterator it, typename iterator::difference_type count_set_parents).

The error can be avoided by altering the order of template instantiation by either forcing instantiation (undesirable; increases compile time for everyone) or rewriting set_parents() without iterator::difference_type.

Users facing this issue may force instantiation in the correct order using

using ItemType = decltype(*std::declval<nlohmann::ordered_json>().items().begin());

before code triggering the compilation failure.

(Found via #3731.)

Reproduction steps

See code example.

Expected vs. actual results

Code should compile.

Minimal code example

void test(nlohmann::detail::iteration_proxy_value<
          nlohmann::detail::iter_impl<nlohmann::ordered_json>> const& val) {
    static_cast<void>(val.value());
}

Error messages

GCC:

error: invalid use of incomplete type 'using nlohmann::json_abi_v3_11_2::basic_json<nlohmann::json_abi_v3_11_2::ordered_map>::iterator = nlohmann::json_abi_v3_11_2::basic_json<nlohmann::json_abi_v3_11_2::ordered_map>::iter_impl<nlohmann::json_abi_v3_11_2::basic_json<nlohmann::json_abi_v3_11_2::ordered_map> >' {aka 'class nlohmann::json_abi_v3_11_2::detail::iter_impl<nlohmann::json_abi_v3_11_2::basic_json<nlohmann::json_abi_v3_11_2::ordered_map> >'}

Clang:

error: no type named 'difference_type' in 'nlohmann::detail::iter_impl<nlohmann::basic_json<nlohmann::ordered_map>>'

MSVC:

error C2039: 'difference_type': is not a member of 'nlohmann::json_abi_v3_11_2::detail::iter_impl<nlohmann::json_abi_v3_11_2::ordered_json>'

Compiler and operating system

Compiler Explorer: GCC (trunk) / Clang (trunk) / MSVC latest

Library version

3.11.2/develop

Validation

Activity

  1. gregmarr commented on Sep 8, 2022

    @gregmarr
    Contributor

    Do you know why this only happens with ordered_json and not the regular json? Is it possible for a user to hit this without using types from the detail namespace?

  2. falbrechtskirchinger commented on Sep 8, 2022

    @falbrechtskirchinger
    ContributorAuthor

    I'm not entirely certain in what way the order of template instantiation changes with ordered_json. I suspect it's related to object_t::key_type and how that is derived in various places. When I deduced the type instead of spelling it out, the error went away. I don't know if it's possible to trigger this error without directly using types from detail, but I think it warrants further investigation.

  3. luistung commented on Sep 15, 2022

    @luistung

    I think template class iter_impl and ordered_json depend on each other when we use nlohmann::detail::iter_impl<nlohmann::ordered_json>. So nlohmann::ordered_json should be explicit instantiate beforehand.

  4. nlohmann commented on Nov 29, 2024

    @nlohmann
    Owner

    Alternatively, I can change the count_set_parents parameter of set_parents to int or std::ptrdiff_t - then your example compiles.

  5. added this to the Release 3.12.1 milestone on May 14, 2026
  6. added a commit that references this issue on May 14, 2026
    93e49de
  7. added a commit that references this issue on May 19, 2026
    ea3c4d0
  8. added a commit that references this issue on Sep 21, 2026
    1c2653d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions