-
-
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
Remove undefined behavior for const operator[] #2111
Comments
A lot of people were annoyed by the behavior of |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
For the record, I suggested this exact possibility in this thread back in 2017: #289 (comment) would still love for it to make it into the official library in some form or another, for all the same reasons that I went into back then. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Hello, It is a bit painful to have to check every key, especially when the researched value is deep. This proposal (just returning some kind of sentinel) would allow to search deep values in a user-friendly way , ex:
You mentioned you would consider this after the 3.8.0. Do you have some opinion on this request ? |
In most places in the library, when using a non-const object, attempting to access a non-existent array value or key via the operator[] methods will result in creation of those methods. On the other hand, if the basic_json object is const qualified, then for arrays operator[] results in undefined behavior, where for objects it acts more like the at() function with an exception.
I'd like to propose modifying the implementations of the const overloads of operator[] to instead return a const-reference to a static instance of an empty basic_json<> object. There are a handful of ways to do this (static data member or static singleton method). This would mean that you can't easily slip into undefined behavior/exception by adding const qualifying a json object.
The text was updated successfully, but these errors were encountered: