-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Bug in basic_json::operator[] const overload #135
Comments
The problem is that json::operator[] const simply chains to array_t/object_t::operator[] using m_value.array/object, which is a const pointer to a non-const object.
This will fix it:
For object, it's a bit more complicated, since as you said,map doesn't have operator[] const:
|
This problem is very misleading. I use a json object for parameter input/output and it ends up with a lot of null objects. |
I'm on holiday right now. I'll check this when I come back. |
Changing a const object is definitely not OK. However, I think it was a mistake to offer an I see two options here:
Even with the downside, I would prefer option 2, because it is closer to the other STL containers. What do you think? |
I would prefer 2 as well. |
+1 for option 2 |
I'll see if I can realize option 2 this weekend. |
I need unchecked The problem with
IIUC I have been left without an alternative/workaround for my use case. Is there one? In a nutshell what I need is:
That is basically what I understand that in the previous state For me, using the standard I can understand the rationale of "doing what |
It was a good idea to implement a const version of operator[] it in the first place. I was a pity that this implementation was flawed. It was a mistake to remove the const version completely. This commit re-introduces the const version. My apologies for all the inconvenience.
Hi all, just to let you know, I reintroduced the const operator[], see http://nlohmann.github.io/json/classnlohmann_1_1basic__json_a8e34088252a3ee6b2377f3a1f26dd1ba.html#a8e34088252a3ee6b2377f3a1f26dd1ba. Sorry for all the inconvenience. |
The following code
prints
Allowing a const object to change like this is not a good idea.
I think this is a bug: either the const overload of
operator[]
throws if the key cannot be found, or there is no such const overload (e.g. std::map does not have it).The text was updated successfully, but these errors were encountered: