json icon indicating copy to clipboard operation
json copied to clipboard

Using iteration_proxy_value with ordered_json fails to compile due to incomplete type

Open falbrechtskirchinger opened this issue 3 years ago • 4 comments

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

falbrechtskirchinger avatar Sep 08 '22 09:09 falbrechtskirchinger

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?

gregmarr avatar Sep 08 '22 11:09 gregmarr

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.

falbrechtskirchinger avatar Sep 08 '22 12:09 falbrechtskirchinger

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.

luistung avatar Sep 15 '22 19:09 luistung