-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crash when traversing over items() of temporary json objects #2040
Comments
When you create I am not sure how to fix this. |
However, it works fine when traversing over temporary json array: const auto arr = nlohmann::json::parse(R"([
"key1",
"key2"
])");
std::cout << "not crash1:" << std::endl;
auto rvalue = [&]() -> nlohmann::json { return arr; };
for (const auto& item : rvalue()) {
std::cout << item << "\n";
}
std::cout << "not crash2:" << std::endl;
auto crvalue = [&]() -> const nlohmann::json { return arr; };
for (const auto& item : crvalue()) {
std::cout << item << "\n";
} |
Could we temporarily disable iteration_proxy<iterator> items() & noexcept
{
return iteration_proxy<iterator>(*this);
}
iteration_proxy<const_iterator> items() const & noexcept
{
return iteration_proxy<const_iterator>(*this);
}
iteration_proxy<iterator> items() && noexcept
{
static_assert(false_v<iterator>, "items() of rvalue is not supported.");
}
iteration_proxy<const_iterator> items() const && noexcept
{
static_assert(false_v<iterator>, "items() of const rvalue is not supported.");
} |
Ref overloads are great, but such fix is a breaking change. Code like
is valid. Though I hope there isn't much code like that. |
I found this to be a undefined behavior
So, is there a way to work around in C++ 11/14/17 ? 😂 |
Simple workaround exists
it doesn't answer if |
Any ideas how to proceed here? Maybe adding a note to the documentation of |
I think so. I mean proxy nature of |
I hope so. It would be very helpful, especially for the users of dynamic languages (like Python). |
items()
of temporary json objectspair.value()
(https://godbolt.org/z/TTQFHW)pair.value()
returns0.1
or0.2
json.hpp:14028: void nlohmann::detail::serializer<BasicJsonType>::dump(const BasicJsonType&, bool, bool, unsigned int, unsigned int) [with BasicJsonType = nlohmann::basic_json<>]: Assertion ‘false' failed.
pair.value()
returns null on Compiler Explorer. (https://godbolt.org/z/TTQFHW)develop
branch?The text was updated successfully, but these errors were encountered: